Skip to Content
Menu
This question has been flagged
1 Reply
4339 Views

Hi all, I'm getting little bit confused on how to do inheritance.

The case is that I want inheritance model "exist_A" and use it on model "new_B", of course, I want add some new field in new_B. In terms of database, I would like store them into model new_B. In another words, without affecting exist_A database. Is there any way to achieve this?

What I have tried is following:

PY file:

Class new_B(models.Model):

    # _name = new_B # if I define "_name", it shows that field "new_cloumn" not in " exist_A "

    _inherit = 'exist_A' # also I tried _inherits = { 'hr.expense':'id' }, but not sure this is the case I should use "_inherits"

    new_cloumn = fields.Char()

XML file:

<record id="tree_view_inherit_new_B" model="ir.ui.view">

    <field name="name"> tree.view.inherit.new_B </field>

    <field name="model">exist_A</field>

    <field name="inherit_id" ref=" exist_A.tree_view"/>

    <field name="mode">primary</field> <!-- other similar question's solution, but it is not working for me -->

    <field name="arch" type="xml">

        <xpath expr="//field[@name='name']" position="after">

            <field name="new_cloumn"/>

        </xpath>

    </field>

</record>


Any help will be appreciated! Thanks!

Avatar
Discard
Best Answer

Hi David,

You should use _inherits / delegation  in your case which will not affect the parent table in the database.

Difference between _inherit and _inherits

Sudhir Arya
ERP Harbor Consulting Services
Skype:sudhir@erpharbor.com
Website: http://www.erpharbor.com
Avatar
Discard
Author

Thanks for reply, for using _inherits, I still got error saying that it can't find field new_cloumn. This is how I do it.

PY file changed to:

_name = new_B

_inherits = { 'exist_A':'new_cloumn' }

new_cloumn = fields.Many2one('exist_A')

Remove / comment "_inherits" and add delegate=True in your field.

EX: new_cloumn = fields.Many2one('exist_A', delegate=True)

Author

This is how it looks like after edit it:

PY file:

_name='new_B'

new_cloumn = fields.Many2one('exist_A', delegate=True)

Result: Error remains the same...

In terms of XML: for where need to refer to a model, I use exist_A, not new_B, does it matter? E.g. action: <field name="res_model">exist_A</field> view: <field name="model">exist_A</field>

Related Posts Replies Views Activity
1
Aug 24
421
2
Nov 24
749
3
Oct 23
13195
2
Feb 23
1229
1
Dec 22
1371