others linux服务器运维 django3 监控 k8s golang 数据库 大数据 前端 devops 理论基础 java oracle 运维日志

ODOO开发模块扩展

访问量:1306 创建时间:2021-07-15

课 程 地 址:https://www.bilibili.com/video/BV1m54y167ET?from=search&seid=9421485045063291271 课 件 文 档 地 址:http://www.odoodev.cn/

odoo提供了三种不同的机制以模块化方式扩展模型:

扩展(Extension)

当使用_inherit但不使用_name时,新模型将替换现有模型,本质上将其扩展。一般是使用增加新字段或方法,或者自定义现有方法内容:

    _name = 'extension.0'
    _description = 'Extension1 zero'

    name = fields.Char(default='A')

class Extension1(models.Model):
    _inherit = 'extension.0'
    description = fields.Char(default='Extended')

模型继承(Classical inheritance)

从已存在的模型创建新的模型,在新的模型添加一些信息,原模型保存不变。 新模型拥有被继承模型的属性和方法,如果字段或方法名称相同则替换旧模型中的属性和方法,旧模型保持不变。

class Inheritance0(models.Model):
    _name = 'inheritance.0'
    _description = 'Inheritance Zero'

    name = fields.Char()

    def call(self):
        return self.check("model 0")
    def check(self,s):
        return "This is {} record {}".format(s,self.name)

class Inheritance1(models.Model):
    _name = 'inheritance.1'
    _inherit = 'inheritance.0'
    _description = 'Inheritance One'

    def call(self):
        return self.check("model 1")

使用如下:

a=env['inheritance.0'].create({'name':'A'})
b=env['inheritance.1'].create({'name':'B'})
a.call()
b.call()
#结果
"This is model 0 record A"
"This is model 1 record B"

Delegation

视图的继承

<record id="china_city.res_country_tree_view" model="ir.ui.view">
        <field name="name">res_country_tree_view</field>
        <field name="model">res.country</field>
        <field name="inherit_id" ref="base.view_country_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//tree" position="attributes">
                <attribute name="create">true</attribute>
            </xpath>
            <xpath expr="//tree/field[@name='code']" position="after">
                <field name="phone_code"/>
            </xpath>
            <field name="code" position="before">
                <field name="currency_id"/>
            </field>
        </field>
    </record>

在标签上直接使用属性 position,其取值有一下几个:

登陆评论: 使用GITHUB登陆