This question has been flagged
2 Replies
3028 Views

I am a newbie trying to create a product book by inheriting from product object. I have modelled the class as follows

class book( osv.osv):

    _name = 'book'
    _inherits = {'product.product': 'name_template'}
    _columns = {
        'isbn': fields.char('ISBN', size=50),
    }

Now when i try to inherit a view for this object from the normal product form by doing something like this

      <record model="ir.ui.view" id="view_book_form">
            <field name="name">test.books.view</field>
            <field name="model">book</field>
            <field name="inherit_id" ref="product.product_normal_form_view"/>
            <field name="arch" type="xml">
                 <field name="name" position="after">
                        <field name="isbn"/>
                 </field>
            </field>
        </record>

I get errors saying that onchange event is not there in the new class. How should i achieve this? If i alternately try to use the _inherit tag, things seem to be working fine. 

 

Avatar
Discard
Best Answer

Use This Below Way....

class book( osv.osv):

    _name = 'book'
    _inherits = {'product.product':'product_id'}
    _columns = {
        'isbn': fields.char('ISBN', size=50),

        'product_id':fields.many2one('product.product', Product, ondelete="cascade")
    }

 

<record model="ir.ui.view" id="view_book_form">
            <field name="name">test.books.view</field>
            <field name="model">book</field>
              <field name="arch" type="xml">

                  <form string="Course Registration" version="7.0">
                        <field name="isbn"/>
                        <!-- And All other needed product's fields you can define here directly , see example name, type etc.. -->

                        <field name="name"/> 

                       <field name="type"/>
                 </field>
            </field>
        </record>

 

Avatar
Discard
Author

Thanks for your reply. But this is not equivalent to inheriting the views. I just want to add the new fields in the inherited view

Bro. then just take class product_product(osv.osv): _inherit = 'product.product' _columns={ #Add ur field }

Author

Ok Thanks. I was not doing that thinking about the impact it would have on interaction with other modules like accounts, Inventory etc.since this would create new tables. Would they behave similarly?

dont warry bro , it will not create new table , if your code like above 'Comment' using _inherit = 'product.product' then it will just add new field in product_product table of your database, if you give attribute _table_name then only it will create new table of given name. you can test it.

Best Answer

There are two type of Inheritence :

1. Object Inheritance - _inherit

2. Inheritance by Delegation - _inherits

So am guessing you want to inherit fields from Product Object and derive your custom module  "book" object .. If that is the case, do use "inherit"

Your code goes like this:

class book( osv.osv):

    _name = 'book'
    _inherit = 'product.product'

...

Avatar
Discard