The importance of correctly setting priority on inherited views

The sequence of loading the views in Odoo is critical in the inheritance mechanism. The changes in the views are always applied following their order by priority​ and ID​. This means the views with the same priority will always be ordered by the ID​ that they have in the database. This makes the order of installing the modules in the database important for properly loading the views. In general, this is a problematic approach because not always we can have control over the sequence the modules are installed, especially when the system has multiple external modules from different sources. Basically, that is why sometimes some view modifications can work in some instances while failing in others.

In order to have more control over the sequence of loading the views in the system it is added priority​ field. Setting up the right value in this field can prevent many of the issues related to inheritance.

The value for the priority​ field mainly depends on three key factors:

  • The view that needs to be inherited,
  • The existing inheritance that the base view has
  • The requirements for the new view

In most of the cases having the biggest priority for the new view can be the right choice, however, there are cases when this is not the best option. Therefore it is always important to do a detailed analysis of the original view and its existing inheritance and on base of that to decide where to insert the new view in the process.

A common issue with view inheritance is when an element from the base view is removed in one of its inherited views while in the new view, this element is used as a position attribute. In this case, if the new view is added with the biggest priority​ value the server will start to complain because the element is removed from the base view before the changes for the new view are applied.

The priority of a new view can be set in the following way:

    <record id="view_order_form" model="ir.ui.view">
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
		<field name="priority">18</field>
        <field name="arch" type="xml">
            <!-- Here goes the customization part -->

The same things related to priority apply when working with QWeb templates. The priority​ value for a new QWeb templates can be set as shown in the example below:

	<template id="products_description" inherit_id="website_sale.products" priority="55"><!-- Here goes the customization part -->

The priority​ of a view can be updated from the UI using the "Views" menu in Settings or "Edit View" option in Developer Mode. The label for the priority​ field is "Sequence".