This question has been flagged

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'

 

        @api.one

        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.

Avatar
Discard
Best Answer

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..

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

Hope this will helps.

Rgds,

Anil.



Avatar
Discard
Author Best Answer
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?





Avatar
Discard

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

Author

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