Odoo 11 Development Essentials(Third Edition)
上QQ阅读APP看书,第一时间看更新

Extending views

Forms, lists, and search views are defined using the arch XML structures. To extend views, we need a way to modify this XML. This means locating XML elements and then introducing modifications at those points.

The XML record for view inheritance is just like the one for regular views, but also using the inherit_id attribute, with a reference to the view to be extended.

As an example of this, we will extend the Partner view to make the todo_ids field visible, showing all the Tasks that person is involved in.

The first thing to do is to find the XML ID for the view to be extended. We can find that by looking up the view in the Settings app and the Technical | User Interface | Views menu. The XML ID for the Partner form is base.view_partner_form. While there, we should also find the XML element where we want to insert our changes. We will be adding the list of collaborators at the end of the right columns, after the Language field. We usually identify the element to use by its name attribute. In this case, we have <field name="lang" />.

We will add an XML file for the extensions made to the Partner views, views/res_partner_view.xml, with this content:

<?xml version="1.0"?>
<odoo>
<record id="my_inherited_view" model="ir.ui.view">
<field name="name">Add To-Dos to Partner Form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="lang" position="after">
<field name="todo_ids" />
</field>
</field>
</record>
</odoo>

The inherit_id record field identifies the view to be extended by referring to its external identifier using the special ref attribute. External identifiers will be discussed in more detail in Chapter 5Import, Export, and Module Data.

Usually, XPath is used to locate XML elements. Odoo also uses XPath for this, but it also has the simpler form we just used available. View extension and XPath are discussed further in Chapter 9, Backend Views - Design the User Interface.

The other view types, such as list and search views, also have an arch field and can be extended the same way as form views can.