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.

We can get more information by clicking on the “More” button in the bottom left of the screen

The most important part is the Related Field in Advanced Properties. The field name is partner_id.tz and this means that:

  1. The linking field is partner_id
    • This links to Contacts (res.partner)
  2. The related field is tz on res.partner. This is a Selection Field (user will select a timezone from a hard-coded list).

The field type for the Reference Field must also be a Selection Field.

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).

Other relational fields can be added, and it’s common to use this with a Many2One field.

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 there’s a problem! 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

Disable creation and opening

Select “Disable creation” and “Disable opening”.

This is the result:

Now users can only select from the existing list of countries, and they also 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 True and False {'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.

4 thoughts on “Adding relational fields using Odoo Studio

  1. First of all, I would like to thank you for taking the time to create such a great resource for the Odoo community.
    We are on odoo V12 and so we don’t have the ability to search for the related field when creating them. But even after we upgrade to V13 it would be helpful to understand the logic used to find the path to the target field.
    I know how to lookup the tactical name and model of the target field. By a long mouse over while in developer mode. But it is very confusing in studio to find that field via the related field cascading menu.
    As a concrete example, I am trying to add the BOM name and number on top of pick list report. But I haven’t found the way to do that through the related field menu.

    Like

    1. Yes, it’s not easy to find all the fields you might need. I normally do it by ‘trial and error’ – dragging a field and then checking whether it’s correct or not.

      I’m not so familiar with the Manufacturing application, but it seems that the BOM name should be
      (Production Order) > Bill of Material > Reference

      Like

  2. Hi Chris, great information here, I’m trying to add a vendor field and vendor price on the sales order line, and while I managed to do this with related fields it just give me the first vendor listed in the purchase tab of the product, is there any way to do this where I can select the vendor for the product when I have more than one?

    Thanks in advance, your blog has been incredibly helpful!

    Like

  3. Hi,
    Is it possible to add many2many field as a related field on the other form?
    For example I have an Employee with a skillset (set as tags) in Employee Profile. And I want to display all these Employee’s skills on the other form (tags with skills, read only). How can I do that? Thanks!

    Like

Leave a comment