Accessing 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 doesn't return an error but just returns False. Because of this, we can traverse records using dot-notation without worrying about errors from empty values, as shown here:

>>> self.company_id.parent_id
res.company()

>>> self.company_id.parent_id.name
False

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.131