This question has been flagged
3 Replies
10068 Views

i need to get the relation of odoo selection field value and string.

which table is storing the data?

Avatar
Discard
Best Answer

Normally It does not store the values in the database. They are defined in python and you could access to those values in the model like:

self.env['model_name']._fields[field_name].selection
Avatar
Discard
Best Answer

In selection field, Odoo does not store the label of selection field.

selection_field = fields.Selection([("option_1", "Label_1"), ("option_2", "Label_2")])

Odoo stores "option_1" or "option_2"  in database. Now if the label has t be fetched, we can write something like,

current_state = dict(self._fields["state"].selection).get(self.state)

where, "state" is the name of field defined in the model which can be anything. And if you print self._fields, you will get a frozen dict which contains the key and value filled in the record set.

I hope it will help you.

Avatar
Discard
Best Answer

Hello willie ho,

The value was supposed to be stored in the 'selection' field of the 'ir_model_fields' table (by using the create and write methods of 'ir.model.fields'), but records in that table are actually inserted by direct SQL statements during module installation and the 'selection' column is omitted there, see here: https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L357

You can run the following SQL queries to verify that the selection column is not populated:

-- 'ir.model.fields' has some selection fields, not stored in the DB 
SELECT id, model, ttype, name, field_description, selection
FROM ir_model_fields
WHERE model like 'ir.model.fields'
ORDER BY name;
-- selection is either NULL or empty for all fields in the DB
SELECT id, model, ttype, name, field_description, selection, domain
FROM ir_model_fields
WHERE selection IS NOT NULL
AND selection NOT LIKE ''
ORDER BY name;

Anyway, you can directly get the 'selection', and also some other field attributes, by directly introspecting the field object, please see the following example

model_name = 'ir.model.fields' 
selection_field = 'on_delete'
model_obj = self.pool[model_name]
field_selection = model_obj._columns[selection_field].selection

In the above example I'm obtaining the 'selection' value for the following field: https://github.com/odoo/odoo/blob/8.0/openerp/addons/base/ir/ir_model.py#L267

Not storing the 'selection' values in the database is related to the fact that you can either assign a list, a method reference or even a method name (new since Odoo 8.0) for selection field values. At runtime, the value is appropriately evaluated depending on its type: https://github.com/odoo/odoo/blob/8.0/openerp/fields.py#L1363

See this link for a reference: https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.fields.Selection

Regards.

Avatar
Discard