Setting values for relation fields

We have just seen how to set a value on a many-to-one relation field, such as user_id, using the eval attribute with a ref() function. But there is a simpler way.

The <field> element also has a ref attribute to set the value for a many-to-one field, using an external identifier. With this, we can set the value for user_id using just the following:

<field name="user_id" ref="base.user_demo" /> 

For one-to-many and many-to-many fields, a list of related IDs is expected, so a different syntax is needed; Odoo provides a special syntax to write to these types of fields.

The following example, taken from the official Fleet app, replaces the list of related records of a tag_ids field:

<field name="tag_ids" eval="[(6,0, ...

Get Odoo 11 Development Essentials - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.