- 在被继承模块的同级文件夹中创建继承模块的文件夹
- 创建
__init__.py
和__manifest__.py
__manifest__.py
中depends属性中加上被继承的模块名'depends': ['module_name'],
in-place继承
in-place继承实际上就是对已有模型的引用并在原处做修改。
- 在py文件里定义类,使用
_inherit
属性即可,_inherit = 'module_name'
。 - 这种继承会获得原有模型的所有属性,如果想更改已有字段、属性甚至方法,直接在此模型里重写即可,如果想添加新的字段、属性和方法,直接在此模型写即可。
- 这种继承中所做的所有更改都会作用在原有模型上。
视图和数据的继承
视图的继承与修改
- 添加
inherit_id
属性,inherit_id
记录字段通过ref
属性指向继承视图的外部标识符,<field name="inherit_id" ref="module_name.view_name" />
,ref
可以将指定视图的外部标识符转换成数据库里的id。 - 定位节点的方法有三种,官方文档是这样讲的:
There are three types of element locators for matching a target
element:
- An
xpath
element with anexpr
attribute.expr
is an XPath expression applied to the currentarch
, the first node it finds is the match- a
field
element with aname
attribute, matches the firstfield
with thesamename
. All other attributes are ignored during matching- any other element: the first element with the same name and identical attributes(ignoring
position
andversion
attributes) is matched
(1) 最简单的方法就是第二种——使用唯一标识属性,也就是field
中的name
字段。
在arch里定义:
<field na