How it works...

The crucial field here is, as you've probably guessed, inherit_id. You need to pass it the XML ID of the view you want to modify (inherit from). The arch field contains instructions on how to modify the existing XML nodes within the view you're inheriting from. You should actually think of the whole process as simple XML processing, because all the semantic parts only come a lot later.

The most canonical instruction within the arch field of an inherited view is the field element, which has the required attributes, name and position. As you can have every field only once on a form, the name already uniquely identifies a field. With the position attribute, we can place whatever we put within the field element, either before, inside, or after the field we named. The default is inside, but for readability, you should always name the position you require. Remember that we're not talking semantics here; this is about the position in the XML tree relative to the field we have named. How this will be rendered afterwards is a completely different matter.

Step 2 demonstrates a different approach. The xpath element selects the first element that matches the XPath expression named in the expr attribute. Here, the position attribute tells the processor where to put the contents of the xpath element.

Step 3 shows how you can change the position of an element. This option was introduced in version 12 and it is rarely used. In our example, we moved the phone field to come after the email field with the position=move option.

XPath might look somewhat scary, but is a very efficient means of selecting the node you need to work on. Take the time to look through some simple expressions; it's worth it. You'll likely stumble upon the term context node, to which some expressions are relative. In Odoo's view inheritance, this is always the root element of the view you're inheriting from.

For all the other elements found in the arch field of an inheriting view, the processor looks for the first element with the same node name and matching attributes (with the attribute position excluded, as this is part of the instruction). Use this only in cases where it is very unlikely that this combination is not unique, such as a group element combined with a name attribute.

Note that you can have as many instruction elements within the arch field as you need. We only used one per inherited view because there's nothing else we want to change currently.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset