How it works...

Reference fields are similar to many-to-one fields, except that they allow the user to select the model to link to.

The target model is selectable from a list that's provided by the selection attribute. The selection attribute must be a list of two element tuples, where the first is the model's internal identifier, and the second is a text description for it.

Here's an example:

[('res.users', 'User'), ('res.partner', 'Partner')] 

However, rather than providing a fixed list, we can use most common models. For simplicity, we are using all the models that have the messaging feature. Using the _referencable_models method, we provided a model list dynamically.

Our recipe started by providing a function to browse all the model records that can be referenced to dynamically build a list that will be provided to the selection attribute. Although both forms are allowed, we declared the function name inside quotes, instead of directly referencing the function without quotes. This is more flexible, and it allows for the referenced function to be defined only later in the code, for example, which is something that is not possible when using a direct reference.

The function needs the @api.model decorator because it operates on the model level, not on the recordset level.

While this feature looks nice, it comes with a significant execution overhead. Displaying the Reference fields for a large number of records (for instance, in a list view) can create heavy database loads, as each value has to be looked up in a separate query. It is also unable to take advantage of database referential integrity, unlike regular relation fields.
..................Content has been hidden....................

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