Enable Dark Mode!
how-to-override-create-write-and-unlink-methods-in-odoo-16.jpg
By: Arunima V

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

Technical Odoo 16

Odoo is a robust open-source business application suite that provides a wide range of functionalities for various business operations. One of its key features is the ability to customize and extend its functionality to suit specific business needs. In this blog, we will explore how to override the create, write, and unlink methods in Odoo 16, allowing you to tailor the behavior of your Odoo modules without modifying the core code.

In Odoo, Create, Write, and Unlink are fundamental database operations. Overriding these methods allows you to intercept and customize the behavior when records are created, updated, or deleted in your Odoo models.

Overriding the Create Method

The create method is called when a new record is being created in a model.

Let’s check an example of overriding the create method of ‘sale.order’ model, to show a Usererror if the partner is also a vendor.

from odoo import models, api
from odoo.exceptions import UserError
class SaleOrder(models.Model):
    _inherit = 'sale.order'
    @api.model
    def create(self, vals):
    # Call the original create method to create the record
    order = super(SaleOrder, self).create(vals)
    # Check if the partner is also a vendor (you may need to customize         this condition)
    if order.partner_id and order.partner_id.is_vendor:
        raise exceptions.UserError("Partner is also a vendor. Cannot create the sale order.")
    return order

In the create method, you can customize the behavior before and after the record creation, such as data validation or setting default values.

In this example, first inherit the sale.order model using _inherit so that we can extend its functionality.

Then, override the create method using the @api.model decorator. Inside this method, we first call the original create method using super to create the sale order record.

Then, we can check if the partner associated with the sale order (accessed using order.partner_id) meets your condition for being a vendor. You should customize the condition based on your specific requirements. In this example, I've used a hypothetical attribute is_vendor on the partner model to check if the partner is a vendor.

If the condition is met, it will raise a UserError with a message indicating that the partner is also a vendor, and we prevent the creation of the sale order.

Overriding the Write method

The write method is called when an existing record is updated.

Let’s modify the behavior of updating the name field of a partner by automatically capitalizing the text.

from odoo import models, api
class CustomResPartner(models.Model):
_inherit = 'res.partner'
@api.model
def write(self, vals):
    # Add custom logic here
    # For example, let's capitalize the 'name' field when updating a partner
    if 'name' in vals and vals['name']:
        vals['name'] = vals['name'].capitalize()
     
    # Call the original write method to update the record
    return super(CustomResPartner, self).write(vals)

In this example, we're extending the res.partner model and overriding the write method. When updating a partner record, this method checks if the name field is being modified and ensures that its value is always capitalized.

First, inherit the res.partner model to extend its functionality. While overriding the create method, inside the method we can add ourt logic. In our case to capitalize the name of the partner.

After that, call the original write method to update the record.

In the write method, you can customize the behavior before or after the record creation, according to your business needs.

Overriding the Unlink method

The unlink method is called when a record is deleted.

Let’ 's take an example of returning a usererror while deleting a partner record

from odoo import models, api
from odoo.exceptions import UserError
class CustomResPartner(models.Model):
_inherit = 'res.partner'
def unlink(self):
      # Raise a UserError when attempting to delete any partner record
      raise UserError("You cannot delete partner records.")

In this above code, we are inheriting the res.partner record to extend the model functionality.

We override the unlink method. The unlink method is called when someone tries to delete a partner record. In the overridden unlink method, we raise a UserError.

Overriding the create, write, and unlink methods in Odoo 16 is a powerful way to customize the behavior of your Odoo modules without modifying the core code. These methods allow you to intercept and tailor the creation, update, and deletion of records in your models to meet specific business requirements.

To read more about setting up & use system parameters in the Odoo 15, refer to our blog How to Set Up & Use System Parameters in the Odoo 15


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



0
Comments



Leave a comment



whatsapp
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