Using relational fields

As we saw earlier, models can have relational fields: many-to-one, one-to-many, and many-to-many. These field types have recordsets as values.

In the case of many-to-one, the value can be a singleton or an empty recordset. In both cases, we can directly access their field values. As an example, the following instructions are correct and safe:

>>> self.company_id
res.company(1,)
>>> self.company_id.name
'YourCompany'
>>> self.company_id.currency_id
res.currency(1,)
>>> self.company_id.currency_id.name
'EUR'

Conveniently, an empty recordset also behaves like singleton, and accessing its fields does not return an error but just returns False. Because of this, we can traverse records using dot notation without worrying ...

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.