Adding a custom field to an existing form is a common customization, and it can be done from the user interface, without the need to create a custom module.
For our to-do application, we want to select a group of people that will be able to collaborate on to-do items. We will identify them by setting a flag on their partner form. For that, we will add an Is Work Team?
flag to the partner model.
The partner model is part of the Odoo core, and is available even if you haven't installed any apps yet. However, you may not have a menu option available to visit it. A simple way to add one is to install the Contacts
application. Open the Apps
top menu, look up this application, and install it, if you have not already done so:
After this, the Contacts
application top menu option will be available.
To visit a Model's definition, with the developer mode enabled, in the Settings
application go to the Technical
| Database Structure
| Models
menu item.
Look up the Model
having res.partner
. The corresponding Model Description
should be Contact
. Click on it to open the form view, and you will see all the specific details about the partner model, including the field list:
Now, Edit
the form and click on the Add a line
button at the bottom of the Fields
list. A pop-up window will be presented for new field creation.
Let's fill in the configuration:
Field Name
:x_is_work_team
Field Label
:Is Work Team?
Field Type
:b
oolean
The Field Name
must start with x_
. This is mandatory for Models
and Fields
created directly through the user interface. Customizations made through add-on modules don't have this limitation.
That's it. Click save and close, and our new field should have been added to the Fields
list. Chances are that this model has more than 80 fields, and so you might need to navigate to the next page of the Fields
list to see it. To do so, use the right arrow in the upper-left corner of the Fields
list.
Now, click on the upper-left save button to make this change permanent.
Our new field is now available in the partners model, but it is not yet visible to users. For that, we need to add it to the corresponding views.
Still on the Model
havingres.partner
form, click on the Views
tab, and we will be able to see all the view definitions for the model. As you can see, each view is a record in the database. Changing or adding view records is immediately effective, and will be visible the next time that view is reloaded:
There are a few important things to note in the Views
list.
We can see that there are several View Type
, such as Form
, Tree
, Search,
or Kanban
. The Search
views are actually definitions of the filters available in the upper-right search box. The other view types are the different ways the data can be displayed. The basic ones are Tree
, for list views, and Form
, for the detailed form view.
Note
Both Tree and List can be used to refer to the same view type. They are in fact lists, and the Tree
name exists for historical reasons—in the past, list views used to have a tree hierarchical mode.
You will notice that the same view type can have several definitions. If you sort the list by View Type,
that will be clear.
Each view type, such as Form
, can have one or more base view definitions (the ones with an empty Inherited View
field). Window Actions
, used by menu items, can specify the particular base view to use. If none are defined, the one with the lowest Sequence
is used. You can think of it as being the default view. Clicking on a views line, we will see a form with the view's details, including the Sequence
value:
And each base view may have extensions, called Inherited Views
. Each of these add incremental changes to the corresponding Base view
, for example, adding a field to an existing form.
Note
Extension views can themselves be extended by other extension views. In this case, the later extension is applied to the Base view
after all preceding extensions have already been applied to it.
The res.partner
model in particular can have a crowded view definitions list, since, like ours, many apps need to add extensions to it. An alternative is to access the particular view we want to extend, and edit it from there using the Developer Tools
menu. This can also be used to learn what specific view is being used somewhere in the user interface.
Let's try it now:
- Click on the
Contacts
application to be presented with a list of contact cards, then click on any of the cards, we will navigate to the correspondingForm
view:
- On the
Form
view, click on theDeveloper Tools
menu (the bug icon in the upper-right corner) and select theEdit View: Form
option. This will show the same view details form we saw before inModels
, but positioned on the actual base view definition used here. As you can see, it is theres.partner.form
view. We can see the owner module through theExternal ID
field. In this case, it isbase.view_partner_form
, so we know that this view belongs to thebase
module. In theArchitecture
field, we can see the XML with the baseviewdefinition. We could simply edit the view architecture to add our new field, but in the long run it is not a good idea: this view is owned by an add-on module, and if some time in the future that module is upgraded, these customizations will be overwritten and lost. The proper way to modify a view is to create anInherited View
extension:
- Using the
Inherited Views
tab, we should now create an extension view to add elements to theBase view
:- First, we need to pick an element from the original view to use as the extension point. We can do that by inspecting the
Architecture
inBase view
and choosing an XML element with aname
attribute. Most of the time, this will be a<field>
element. Here, we will pick the<field name="category_id" ...>
element:
- First, we need to pick an element from the original view to use as the extension point. We can do that by inspecting the
- Now, open the
Developer Tools
menu, click on theEdit View: Form
option, select theInherited Views
tab, and click onAdd a line
at the bottom of the list. - A pop-up window,
Create Views which inherit from this one,
will be shown, and we should fill it with the following values:
- Now, open the
View Name
:Contacts - Custom "Is Work Team" flag
Architecture
: Use the following XML:
<field name="category_id" position="after"> <field name="x_is_work_team" /> </field>
- The other important fields, such as the
Model
,View Type,
andInherited View
, already have the correct default values
- The other important fields, such as the
- We can now
Save & Close
, then, in theEdit View: Form
window, clickSave
, and finally close it.
We will be able to see the change done once we reload the Contacts
form view. This means reloading the page in your web browser. In most browsers, you can do that by pressing F5.
If we now visit again a contact form, we should see the new field on the left-hand side, below the Tags
field: