Chapter 4 - Odoo 15 Development Book

Extending create() and write()

Extending create() method

We can extend the create() function of a model to add any additional functionality while creating a record of the model.

Let’s take an example by adding a 'notes’ field in the ‘sale.order’.Here, users who are not in the ‘Administration/Access Rights’ group will get a ‘UserError ‘ when modifying the notes field.

class SaleOrder(models.Model):
   _inherit = 'sale.order'
   notes = fields.Text(string='Notes')
   def create(self,vals):
       res = super(SaleOrder,self).create(vals)
       if 'notes' in vals and not self.env.user.has_group('base.group_erp_manager'):
           raise UserError(_("You cannot edit the field 'notes'"))
       return res

We can do the same thing as an example of the write() method.Before writing, we check group and presence of the field in the vals to write and raise a UserError exception if there is a problem.

def write(self,vals):
   res = super(SaleOrder,self).write(vals)
   if 'notes' in vals and not self.env.user.has_group('base.group_erp_manager'):
       raise UserError(_("You cannot edit the field 'notes'"))
   return res

Overriding _name_search()

We can search for a record in the Many2one widget by whatever fields in the model by redefining name_search.

Let’s take an example: If we want to search a partner based on phone number or their email also not only it’s name in the Many2one field,

class ResPartner(models.Model):
   _inherit = 'res.partner'
   def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
       args = args or []
       domain = []
       if name:
           domain = ['|', '|', ('name', operator, name), ('phone', operator, name), ('email', operator, name)]
       return self._search(domain + args, limit=limit, access_rights_uid=name_get_uid)

First we need to inherit the res.partner and override the _name_search() function.

We pass most of the arguments that we receive unchanged to the super()implementation of the method:

  • name is a string that contains the value the user has typed so far, the user input.
  • args is either None or a search domain that's used as a prefilter for the possible records. (It can come from the domain parameter of the Many2one relation, for instance.)
  • operator is a string containing the match operator. Generally, you will have 'ilike' or '=' .Customizing how records are searched
  • limit is the maximum number of rows to retrieve.
  • name_get_uid can be used to specify a different user when calling name_get() to compute the strings to display in the widget.


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message