How to Override an Unwanted Super Method in Odoo 14?

How to Override an Unwanted Super Method in Odoo 14?

2 minutes, 48 seconds Read

We can create any number of super methods of a method.  Sometimes we don’t want the execution of some super methods, we can easily override unwanted super methods. 
There are two different ways to override super methods
Override a specific super method
Override all super methods of a method
In odoo , we have to inherit the model to super a method. Normally, a method is super to do some extra functionalities.
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 check how it can be done in odoo 14 
Let’s take an example of the model’s production. product’. 
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.
This is how we can easily override super methods in odoo 14.
[wpcc-iframe loading=”lazy” allowfullscreen=”” allow=”autoplay *; encrypted-media *” src=”https://cdn.iframe.ly/LfAlsZa?iframe=card-small&v=1&app=1″ style=”border: 1px solid rgb(222, 222, 222); box-shadow: rgba(0, 0, 0, 0.06) 0px 1px 3px;”]

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *