Enable Dark Mode!
how-to-override-create-write-and-unlink-methods-in-odoo-19.jpg
By: Nihala KP

How to Override Create, Write, & Unlink Methods in Odoo 19

Technical Odoo 19 Odoo Enterprises Odoo Community

In Odoo, create, write, and unlink methods are used to handle the records while creating, updating, and deleting.

We may need to add customizations according to the business logic. So this function helps to add various customisations and validations while handling the records.

Odoo allows developers to override these methods, so you can add your own custom logic without breaking the standard system behavior. This makes it easier to adapt Odoo to specific business requirements.

In this blog, we will learn how to override the create, write, and unlink methods in Odoo 19, along with some best practices to follow for smooth and efficient implementation.

Overriding the create() Method

The create() method is triggered when a new record is being created. If we need to add validations or several conditions, we can add in this function.

For example, we can add a validation that, while creating a record in the ‘res.partner’ model, can make the mobile field mandatory.

from odoo import models, api, exceptions
class ResPartner(models.Model):
  _inherit = "res.partner"
 
 
  @api.model_create_multi
  def create(self, values):
      # Ensure the 'mobile' field is provided
      if 'mobile' not in values or not values.get('mobile'):
          raise exceptions.ValidationError(
              "Mobile is a mandatory field. Please provide a mobile number.")
      # Call the original create method
      return super(ResPartner, self).create(values)
@api.model_create_multi:

This decorator helps to create multiple records at a time. It ensures the performance of Odoo while creating bulk records.

Mobile Field Validation

if 'mobile' not in values or not values.get('mobile'):
    raise exceptions.ValidationError("Mobile is a mandatory field. Please provide a mobile number.")

Calling the Parent Method

return super(ResPartner, self).create(values)

This calls the original create() method from the res.partner model.

It ensures that Odoo’s standard functionality continues to work as expected while applying the custom validation.

By overriding the create() method in this way, you ensure that every partner record includes a mobile number, helping maintain accurate and complete data in the system.

Overriding the Write Method in Odoo 19

The write() method is triggered when a record is edited. To avoid unnecessary edits in the created records, it is mandatory to keep the validations when a record is edited.

To avoid data inconsistencies, we can still apply validations in write functions also.

from odoo import models, api, exceptions
class ResPartner(models.Model):
   _inherit = "res.partner"
   def write(self, values):
       # Ensure the 'mobile' field is provided and not empty when updating
       if 'mobile' in values and not values['mobile']:
           raise exceptions.ValidationError(
               "Mobile is a mandatory field. Please provide a mobile number.")
       # Call the original write method to update the record
       return super(ResPartner, self).write(values)

def write(self, values):

When a record is edited, this function is called.

Mobile Field Validation

if 'mobile' in values and not values['mobile']:
   raise exceptions.ValidationError("Mobile is a mandatory field. Please provide a mobile number.")

Calling the Parent Method

return super(ResPartner, self).write(values)
  • This calls the original write() method from Odoo to perform the update.
  • This calls the default method and maintains the functionalities as it is.

By overriding the write () function, we can make sure that Odoo’s default functionalities and the updates we gave both are working.

Overriding the unlink() Method in Odoo 19

This function works when a record is deleted. To avoid unnecessary data deletion, we can add validation in this method. We can create such a validation that only administrators can delete the records.

from odoo import models, exceptions
class ResPartner(models.Model):
   _inherit = "res.partner"
   def unlink(self):
       # Allow only admin users to delete records
       if not self.env.user.has_group('base.group_erp_manager'):
           raise exceptions.UserError(
               "Only an administrator can delete partner records."
           )
       return super(ResPartner, self).unlink()

Validating User Access

if not self.env.user.has_group('base.group_erp_manager'):
   raise exceptions.UserError("Only an administrator can delete partner records.")
  • This part of the function checks whether the login user belongs to the ERP Manager.
  • If the user does not belong to the group, then the user cannot delete the record.

Calling the Parent Method

return super(ResPartner, self).unlink()
  • This method calls the default unlink() method
  • The default functionality and also maintain the updates we added

By overriding in this way, we can restrict the user’s access to delete the records and protect the data to maintain data consistency.

Customizing the create(), write(), and unlink() methods in Odoo 19 allows developers to apply business rules, ensure data accuracy, and control access to critical operations. With these overrides, you can enforce required fields during record creation, validate updates, and limit deletion to authorized users.

When implemented correctly, these customizations improve data consistency, system security, and overall reliability, while keeping Odoo aligned with specific business needs.

To read more about How to Override Create, Write, & Unlink Methods in Odoo 18, refer to our blog How to Override Create, Write, & Unlink Methods in Odoo 18.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



Recent Posts

whatsapp_icon
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message