Function Overriding in odoo 14
FUNCTION OVERRIDING IN ODOO 14
When it redefines a function of the base class in a derived class with the same signature i.e., name, return type, and parameter but with a different definition, it is called function overriding.
There are two different ways to override super methods
- Override a specific super method.
- Override all super methods of a method.
Override a specific super method
A method can have different methods, and we can override one super method from a specific class.
Override all super methods of a method
If a method has more than one super method, and don’t want to execute any functionalities of those super methods, then we can override all super methods of that method.
Here we can override the function , we can take the model “product.product”
Step 1:
Create py file with class inherit product.product
eg:
class ProductConstraints(models.Model): _inherit = 'product.product' @api.model def create(self, vals): res = super(ProductProduct, self).create(vals) # generate sequence ref = self.env.ref('webhook.seq_product_auto') res.default_code = ref.with_context( force_company=False).next_by_code( "product.product") or _('New') return super(ProductProduct, self).create(vals) class ProductConstraints(models.Model): _inherit = 'product.template' @api.model def create(self, vals_list): res = super(ProductConstraints, self).create(vals_list) if not res.barcode: raise UserError(_("Add Barcode")) return res
Here we have two super methods of creating () method in model ‘product. product’. Which is the model used to store product details.
The first super method creates a sequence for the ‘product. product’ in default_code’ field which is used for the internal reference number of a product. Automatically generate a sequence for each product at create time itself in creating () super method of class ‘ProductProduct’.
The second super method is used to produce a user error if the barcode number is missing in the product create() method. If the barcode is missing, then the create() method of class ‘ProductConstraints’ raises a user error.
Now let’s check how to override these super methods of the create() method.
Let’s check how to override the create() method of the class ProductProduct. It can be done by inheriting the model’s product. product’ and overriding the create() method. So, sequence numbers for product internal reference will not be created.
class ProductSequence(models.Model): _inherit = 'product.product' @api.model def create(self,vals): return super(ProductProduct, self).create(vals)
Here return super(ProductProduct, self).create(vals) override the super method of create() method of class ProductProduct .
This is how we can easily override an unwanted super method of a method.
Now, let’s check how to override all super methods of a method. Here we can override the super methods of creating () method of the model ‘product. product’.
Both product internal reference sequence functionality and checking of barcode during the creation of product will not work.
class ProductCreate(models.Model): _inherit = 'product.product' @api.model def create(self,vals): return super(models.Model, self).create(vals)
All create() super methods of model ‘product. product’ will be overridden.
Here , return super(models.Model, self).create(vals) override all super methods.