Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
6 Trả lời
53783 Lượt xem

Hello. Is it possible to extend a model via _inherit and extend it with a further inheritance?

For instance, i have made a mixin AbstractModel for tracking and other functions. I'd like to extend sale.order so that it inherits my misc-in. Can this be done?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Yes it is possible to do what you're asking.

class MyMixedInSaleOrder(models.Model):
    _name = 'sale.order'
    _inherit = ['sale.order', 'my.mixin']


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

In Odoo/OpenERP we can inherit or use existing modules object/class/model and views. We can also inherit single field of existing modules. The question is why we need such inheritance.

The purpose of inheritance or why we need inheritance is given below:

  1. To change attributes of some fields which exists on existing/custom model (e.g. making fields readonly,invisible)

  2. To add/modify/delete old or new fields in existing/custom model (e.g. Product, Sales, HR, Fleet Management, Attendance modules model etc)

  3. We can also add buttons in already existing/custom model (form and tree) view by using inheritance

To model inheritance

class groups(models.Model):
 _inherit = 'res.groups'
 # we inherited res.groups model which is Odoo/OpenERP built in model and created two fields in that model.
 field_1 = fields.Char(required=True,string="Field One")
 field_2 = fields.Boolean(default=False,string="Field Two") 

To view inheritance

<openerp>
    <data>
        <record model="ir.ui.view" id="res_group_form_view_inherited">
            <field name="name">res.group.form.view.inherited</field>
            <field name="model">res.groups</field>
            <field name="inherit_id" ref="base.view_groups_form" />
            <!-- <field name="groups_id" eval="[(6, 0, [ref('obe_core.obe_coordinator')])]"/> -->
            <field name="priority" eval="15"/>
            <field name="arch" type="xml">

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

            </field>                               
        </record>
    </data>
</openerp> 

To understanding of code with description please visit: http://learnopenerp.blogspot.com/2018/01/inheritance-in-models-and-views.html

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Nice post.

https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

how can i add more fields to inherit model

Ảnh đại diện
Huỷ bỏ

Hi,

just proceed as Sehrish said above.

class MyModel(models.Model):

_inherit = 'inherited.model'

field_1 = fields.Char()

field_2 = fields.Char()

field_x = fields.Char()

...

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 12 19
3584
0
thg 8 19
3465
2
thg 7 24
14144
1
thg 11 22
3266
1
thg 7 21
8354