Odoo Help


Extending create method for products (product_product), what inherit should be used in the model?

on 7/5/15, 8:14 PM 2,487 views

I am trying to extend the create method for product_product so products which are created without an EAN13 code will have one assigned automatically.

As first step I am trying to define a module which extends the 'create' method of product_product class.

I have defined the following model:

class EanAuto ( models.Model ):

        _inherit = 'product_product'



        def create(self, cr, uid, vals, context=None):

                print "DEBUG: CREATE 1"

                product_template_id = super(product_product, self).create(cr, uid, vals, context=None)

                print "DEBUG: CREATE 2"

                return product_template_id


However it seems it is not correct to inherit product_product. 

Any other comment on the way Super() function is used is also appreciated.


Anil R. Kesariya

--Anil R. Kesariya--
| 7 6 8
Rajkot, India
--Anil R. Kesariya--

Odoo Techno Functional Expert

* Engaged with odoo since May - 2012.

* Customise existing module or develop new module base on customer requirement.

* Played key developer role in many past and running projects.

* Expert in requirement gathering of business and transforming requirement into odoo platform.

* Major expertise in core modules, database, report development and theme development.

* Integration expert.

Linkedin  : https://in.linkedin.com/in/anil-kesariya-97b20a66

Anil R. Kesariya
On 7/6/15, 12:25 AM

Hi E.M.,

You have applied wrong api method or decorator on create method.

When you define or override any method and if that method is passing id or ids as implicit argument than you can use api.one or api.multi.

But here in this case create method dose have any id yet. create it self saying it going to create not created yet. so Id of record is not generated yet.

So, You can use @api.model

You have to use like this:

class your_class(models.Model):
_inherit = 'your.class' #model name which is going to inherit..

def create(self, vals):
#your codee....
    result = super(your_class, self).create(vals)
     #your code.....
    return result

Hope this will helps.



On 7/8/15, 3:24 PM
Following Anil's comments I did this:

# -*- coding: utf-8 -*- from openerp import models, fields, api class EanAuto ( models.Model ): _inherit = 'product.product' @api.model def create(self, vals): print "DEBUG: CREATE 1" new_product = super(EanAuto, self).create(vals) print "DEBUG: CREATE 2" return new_product And it works, I can see print messages and product is normally created.
However, I would like to understand what I did.
The create method for product_product is as follows def create(self, cr, uid, vals, context=None): if context is None: context = {} ctx = dict(context or {}, create_product_product=True) return super(product_product, self).create(cr, uid, vals, context=ctx)

Some questions:
What does Super(EanAuto, self) actually mean?
Why create method from product_product requires cr, uid, vals, context=ctx and the redefined method only uses vals?
What is cr? What is uid?
What is context?

From https://docs.python.org/2/library/functions.html#super: "In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable." For your other questions, you should really take a look at the odoo 8 reference, espacially this part: https://www.odoo.com/documentation/8.0/reference/orm.html#porting-from-the-old-api-to-the-new-api

on 7/9/15, 3:35 AM

That link has helped me a lot to understand the API and to read other people's answers, which are often in old API style. Thanks

on 7/12/15, 2:25 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 7/5/15, 8:14 PM
Seen: 2487 times
Last updated: 7/9/15, 5:47 PM