This question has been flagged
1 Reply
6631 Views

Here is what I want to do:

in the invoice screen, when creating new invoice, we select partner (customer or supplier). when you start typing characters the system bring partner name starting with those characters. I want the system to search not only name but also partner_code (this column I added to res.partner). So, the solution I though about is to override the display_name which is a compute field to make ti include both partner_code and name, instead of name only. i linked the display_name field with a compute method. But, my problem is that compute method is not triggered at all.

 

Please help me find out why it is not triggered.

 

here is the code:

from openerp import fields, models, api, _ # TODO: make the 'Code and Name' labels in Partner and Product dynamic calling a method so the text can be translatable # -------------------------- Product # noinspection PyPep8Naming class product_template(models.Model): #_name = 'product.template' # this is Odoo object product.product which is related to table product_product _inherit = 'product.template' # TODO: add default_code to tree view product_supplier_code = fields.Char(string=_("Product Supplier Code"), help=_("Product Code that your supplier uses")) product_brand = fields.Char(string=_("Brand Name")) @api.one @api.onchange('default_code') def make_pc_uppercase(self): if (self.default_code): self.default_code = (self.default_code).upper() # TODO: in product module, upon change check uniqueness @api.onchange('product_supplier_code') def make_spc_uppercase(self): if (self.product_supplier_code): self.product_supplier_code = (self.product_supplier_code).upper() class product_product(models.Model): #_name = 'product.product' _inherit = 'product.product' _sql_constraints = [ ('default_code','unique(default_code)',_('Product Code Already Exists !!')) ] class res_partner(models.Model): # _name="res.partner" _inherit = "res.partner" partner_code = fields.Char(string="Partner Code", help=_("Partner (Customer / Supplier) Code")) display_name = fields.Char(string="Name", compute='_display_name_compute') #, search='_search_partner') _sql_constraints = [ ('partner_code','unique(partner_code)',_('Partner Code Already Exists !!')) ] # def _search_partner(self): # return "00000000000000000" # # what? @api.one @api.constrains @api.depends('name', 'partner_code') # this definition is recursive def _display_name_compute(self): self.display_name = self.partner_code + ' - ' + self.name print "===========================================================" # if self.partner_code: # self.display_name = self.partner_code + ' / ' + self.display_name + ' / ' + self.name # else: # self.display_name = '-' + self.name + ' / ' @api.one @api.onchange('partner_code') # @api.constrains('partner_code') def onchange_partner_code(self): if (self.partner_code): self.partner_code = (self.partner_code).upper() records = self.search([('partner_code', '=', self.partner_code)]) print "==========================================================::::::: ", len(records) if len(records) > 1: # if only 1 record is found means we are ok. we just found the record we are creating. raise Warning(_("This Partner Code already exists!")) # if raise XXX is called the method will exit from here and will not continue return True else: print "partner code is still FALSE :::::::::::::::::::::::::::::::::::::" # def redirect_partner_form(self, cr, uid, partner_id, context=None): # search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'odootec_view_res_partner_filter') # value = { # 'domain': "[]", # 'view_type': 'form', # 'view_mode': 'form,tree', # 'res_model': 'res.partner', # 'res_id': int(partner_id), # 'view_id': False, # 'context': context, # 'type': 'ir.actions.act_window', # 'search_view_id': search_view and search_view[1] or False # } # return value

 

Avatar
Discard
Author

Sorry code is scrambled. I posted another question regarding that.

Best Answer

The methods that you should override is name_get and name_search.

name_get will determine what text to display when a record is selected, say in a many2one field.  If not overriden the default will be the _rec_name attribute of the model.  If not specified, the default will be taking the 'name' field.  If not defined, the default will be spewing something like 'model.name,9' where model.name is the model name (e.g. res.partner) and 9 is the database ID (e.g. 'res.partner, 123')

name_search will determine how a string entered to the many2one field will be searched against the records.  Normally, it will search only the field designated as 'name', but you can add additional search criteria such as the 'partner_code' column that you have added.

Note that name_search does not require name_get to be changed to work.  Meaning you can just implement change in name_search alone without changing the displayed name.

You can find example of those methods overriden in product.product model (odoo/addons/product/product.py).  It is still written in v7 API though.

Avatar
Discard
Author

as per 8.8.0 docs name_get method is deprecated, and the alternative is to override display_name field as compute. But my problem is the compute method is not triggered

I've commented in your comment in another questions. From the docs that I've read (lastest is dated 15 Feb 2015), name_get is still the underlying method called by display_name.