python - Odoo扩展与继承

标签 python inheritance odoo odoo-9

Odoo 有三种继承类型,我至少有那么多问题。

<强>1。 “正常”继承 (_inherit)

这对我来说相对直观 - 但他们为什么不直接以 pythonic 方式进行:

ChildClass(ParentClass):

为什么他们有(看似等价的):

ChildClass(model.Model):
     _inherit = 'module.parentclass'

<强>2。扩展

这对我来说毫无意义(因为我不知道你为什么要使用它),下面是一个例子,但任何人都可以给我一个实际的用例。 http://www.odoo.com/documentation/9.0/reference/orm.html#extension

<强>3。委托(delegate)(_inherits)

这对我来说也没有任何意义,它看起来像是一个多重子类化,但只是字段,而不是方法。

问题

  1. 为什么存在 _inherit,与普通子类化相比有什么好处/区别?

  2. 什么时候/为什么要延期?我想我有一个想法,但我相信其他人可以表达得更清楚。

  3. 也许有点什么,为什么是_inherits

最佳答案

我自己一直在搞继承,下面是 odoo 的“经典”继承(将 _inherit 和 _name 传递给 child )和 odoo 的“扩展”示例,仅将 _inherit 传递给 child

_inherits(委托(delegate))太古怪了,我什至不打算测试它。我不明白我将如何使用它 - 文档解释了如何 ( http://www.odoo.com/documentation/9.0/reference/orm.html#delegation ) 如果有人可以解释为什么那会很好,但我不会继续强调它。

模型

class Parent(models.Model):
    _name = 'aidentest.parent'

    first = fields.Char()
    last = fields.Char()

    def call(self):
        return self.check(self.first)

    def check(self, s):
        return "name: {} familia: {}".format(s, self.last)

#normal inheritance of parent
class Child1(models.Model):
    _name = 'aidentest.child1'
    _inherit = 'aidentest.parent'

    first = fields.Char()

    def call(self):
        return self.check(self.first)


#this extends parent
class Child2(models.Model):
    #_name = 'aidentest.child2' #no name - "extension" of inherited model
    _inherit = 'aidentest.parent'

    middle = fields.Char()

    def call(self):
        return self.check(self.first)

控制台

>>> p1 = self.env['aidentest.parent'].create({'first':'mr','last':'dad'})
>>> p1.read()
[{'create_uid': (1, u'Administrator'), 'create_date': '2016-07-14 13:54:23', 'display_name': u'aidentest.parent,3', '__last_update': '2016-07-14 13:54:23', 'write_uid': (1, u'Administrator'), 'middle': False, 'write_date': '2016-07-14 13:54:23', 'last': u'dad', 'id': 3, 'first': u'mr'}]
>>> p1.call()
'name: mr familia: dad'
>>> p1.middle
False  

False 表示该字段存在(通过 Child2 的“扩展名”,但未填充)否则我会得到一个属性错误

>>> c1 = self.env['aidentest.child1'].create({})
>>> c1.first
False
>>> c1.middle  
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'aidentest.child1' object has no attribute 'middle' 

Child1 仅从基类继承,而不是从“扩展”基类继承 - 它忽略了 Child2 对父类的扩展。 Child2 通过添加'middle' 字段扩展父级,Child1 无权访问该字段

>>> c2 = self.env['aidentest.child2'].create({})
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\mamwo\Desktop\odoo\openerp\api.py", line 768, in __getitem__
    return self.registry[model_name]._browse(self, ())
  File "C:\Users\mamwo\Desktop\odoo\openerp\modules\registry.py", line 84, in __getitem__
    return self.models[model_name]
KeyError: 'aidentest.child2'

Extending 模型并不真正存在(没有名称,您无法实例化它),它只是向父级添加内容。

关于python - Odoo扩展与继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38362501/

相关文章:

java - 扩展某个类的通用映射到类

c++ - 子类获取父类(super class)私有(private)变量c++

odoo:用户无法读取对象

python - If 语句定义时出错。 (Python)

python - 如何在azure管道中设置多个图像

python - 如何找到总价

python - 如何创建 RML 报告?

python - 将 svg 保存到临时文件 Python

c# - 具有实现多个接口(interface)的返回类型的方法

ssl - 生成报告后,Odoo 停留在加载屏幕中