Odoo视图继承注意的点

Posted by Ethan on 2019-08-24

授权继承的视图继承

模型继承时,使用授权继承方式,也称代理继承,即应用这样的代码:

1
2
3
book_id = fields.Many2one('training.book', string="书籍", delegate=True)

_inherits = {'training.book': 'book_id'}

在写Form视图继承时,容易忽略的一点是在代码中加入一句:

1
<field name="mode">primary</field>

这样在Form视图中才可以正常显示。
官方解释:

  • View matching
    • if a view is requested by (model, type), the view with the right model and type, mode=primary and the lowest priority is matched
    • when a view is requested by id, if its mode is not primary its closest parent with mode primary is matched

视图继承中position='move’的用法

直接贴代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<record id="view_training_book_copy_form" model="ir.ui.view">
<field name="name">training.book.copy.form</field>
<field name="model">training.book.copy</field>
<field name="mode">primary</field>
<field name="inherit_id" ref="view_training_book_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='author']" position="before">
<field name="book_id"/>
<field name="name"/>
<field name="reference"/>
<field name="book_rented"/>
</xpath>
<xpath expr="//field[@name='ISBN']" position="after">
<field name="year" position="move"/>
</xpath>
</field>
</record>

year字段定义在view_training_book_form视图中,通过position='move'可以将移动到ISBN字段后面,即可以自定义位置。
官方解释:
the position move can be used as a direct child of a spec with a inside, replace, after or before position attribute to move a node.

1
2
3
4
5
6
7
<xpath expr="//@target" position="after">
<xpath expr="//@node" position="move"/>
</xpath>

<field name="target_field" position="after">
<field name="my_field" position="move"/>
</field>