Adding relational fields using Odoo Studio

Odoo Studio lets you add several different field types, including the following relational fields:

Related: adding other field types

If you are not familiar with basic database concepts you should start by reading this introduction.

This is normally used to add more more information from another related Model (database table). It has to be information from a Model that is already linked.

For example, customer is already linked to sales order, so it’s easy to add extra customer-related information to the Sales Order form.

Start by dragging the “Related Field” button to the Form View

In Odoo 13 you will only see many2one fields, all of which represent links to a ‘parent’ Model (database table).

In Odoo 12 (and earlier), you will see a list of all the fields for this Model (db table). many2one fields have the > symbol.

For this example, select customer, and then timezone:

When you add the field, it is given a label of “New Related Field” and a technical name that is a jumble of letters and numbers:

We need to change that to something more meaningful.

Note that after we change the label:

  • the technical field name is also changed
  • the new label is displayed on the screen

We have added the field and the user’s timezone is automatically displayed on sales orders.

Note: in Odoo 12 and earlier, the new Related Field will NOT be set to Read Only, and this would allow users to make changes (in this example, to the customer record). Normally you should set it to Read Only to prevent this (though there might be other cases where it is OK to allow it to be changed).

many2one

This will create a new database relationship (in this example it’s between a product and a country).

If you drag the many2one icon on to the Form View, Odoo will display a list of all models (tables). The model you select will be linked to the current model (as a parent).

You can enter a few characters to search for the Model

Then click on the correct Model.

Again we need to change the Label from “Country” to “Country of Origin”. Odoo will change the Technical Name to match.

Note that you can change the Label at any time, but you can only change the Technical Name at the time you create the field, and not subsequently.

It’s done….but users can click on “Create and Edit” to add new countries. We don’t want that, because all the countries have already been setup.

One very simple solution would be to block create and write access to the model, which would means users would get an error message if they tried to create a new country.

That’s a good idea anyway, but it’s more user-friendly to block the action rather than present the user with an error message.

Fortunately, that’s easy to do in Odoo 13:

Select “Disable creation” and “Disable opening”.

This is the result:

Now users can only select from the existing list of countries, and they cannot use the “External Link” button (see below).

In Odoo 11 and 12 you can manually make a small change to the XML to achieve the same thing:

<field name="x_studio_country_of_origin" 
string="Country of Origin"
options="{'no_create_edit': True},
{'no_quick_create': True},
{'no_open': True}"
/>

Now the drop-down list is displayed without “Create and Edit” and with no options for “Quick Create” and no “External Link” button .  

add-many2one-7

Note: the only options are {'no_open': True} and {'no_open': False}. It’s not possible to add a domain!

External link

The “External Link” button is automatically added for a many2one

This is removed by {'no_open': True} in the above changes to XML

many2many

This is similar to many2one (i.e. creates a new database relationship), except that multiple records can be selected.

These are sometimes displayed as ‘tags’, as shown on Contacts:

The process to add a many2many field is almost the same as many2one (described above), except that you will need to choose a different ‘widget’.

Selection of ‘widgets’ for a many2many field

The default ‘widget’ (many2many) is a list, which is probably not what you want. More information on the other widgets is here.

one2many

This is used if a “parent-child” relationship has already been defined for this table, but is not displayed on the Form View.

How to add a one2many field

Tip: If the relationship doesn’t exist, create a many2one field on the “child” table to setup the link and then come back to add a corresponding one2many.