Odoo Help

Welcome!

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

0

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

By
E.M.
on 7/5/15, 8:14 PM 827 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'

 

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

1

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
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..

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



0
E.M.
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

PY
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

E.M.
on 7/12/15, 2:25 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

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