Reference Fields in Odoo
Odoo has a character type, an integer type, a Boolean type and many others More importantly, Odoo has a relational field that allows for establishing relationships between different models. A reference field will assist in creating a dynamic relationship in a model which will mean that you can select the model based on which the relation is to be created. To define many-to-one, one-to-many, and many-to-many fields, we can preset the target model in which the relationship is to be created. For instance, in a Sale Order, if we contemplate a Many2one field for customers, it will only create a relation between these two models, here we describe the target model which is customers.
Regarding the reference field first, it is possible to select the target model and then the record with subsequent related tables selection. For instance, I have to include a new field named ‘Document Source’ in the Customer model. Here, based on the customers, I might include Purchase Order as a document source, for other customers, I would have to include Sale Order as the source. At times there may be a need to put Invoice as the source. This one, I can define a reference field for this one. Initially, I need to determine the type of the model which can be Purchase, Sale or Invoice, and then select the desired record.
You can directly view a basic instance of the reference field from; Preferences >> Technical >> Menu List.
If we attempt to get into a creation of a Menu Item from here, then we need to define an Action that belongs to this menu. This Action field is a Reference Field. It is so because first, the action model must be chosen, which means selecting whether it is an IR. actions. the report, ir. actions. server, ir. actions. act_window etc After that we can select the particular action of that selected model.
Once you have chosen the model it will then display another option to choose records in the selected model.
It will be displayed as a field with only the record after the save action as they are usually like the Many2one field.
We can verify in which way the reference fields can be defined. For this, I have taken the above example of the ‘Document Source field in Contacts’.
source_document = fields.Reference (selection = [('sale.order', 'Sale Order'),('purchase.order', 'Purchase Order'), ('account.move', 'Invoice')],string = "Source Document")
Here, there is a common field from which one can select a Sale Order, Purchase Order or Invoice for customers.
- Customer with Sale ref.
Customer with Purchase ref.
Customer with Invoice ref.
Also, one can select the values. For the selection from a function, it means that the output of the function is selected from the set. Just recall that I need to access all modules of a field and then choose records from them. Then use
source_document = fields.Reference (selection = '_ select_target_model', string = "Source Document") @ api.model def _select_target_model (self): models = self.env ['ir.model']. search ([]) return [(model.model, model.name) for model in models]
You might select from all the models that are displayed.
The database will store model_name>, id as a reference field.
Eg: sale. order, 10
The reference fields are accessible in the same way as a Many2one file.
We can now examine the process of entering data into a reference field.
For example, I would like the customer’s most recent sale order to be referenced in this “Source Document” box.
def _selection_target_model (self): model = self.env ['ir.model']. search ([('model', '=', 'sale.order')]) return [(model.model, model.name) ] source_document = fields.Reference (selection = '_ selection_target_model', string ="Source Document",compute = "_ compute_sale_order", inverse = "_ set_sale_order") def _compute_sale_order (self): order = self.env ['sale.order'] .search ([('partner_id', '=', self.id)], order = 'id desc', limit = 1) for rec in self: if order: rec.source_document = '% s,% s'% ('sale.order', order.id) else: rec.source_document = False def _set_sale_order (self): order = self.env ['sale.order']. search ([('partner_id', '=', self.id)], order = 'id
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote