Selection field: How to get value from human-readable value?

For a Selection field, how do I get from the (translated) human-readable value to the value to be put into the field?

I’m implementing a custom importer reading xslx-files in a very special format. When importing data, user expect to be able to put the (translated) human-readable value into the sheet. Now when creating the objects, I need to convert this into the “machine-readable” value.

I’m aware of Selection.get_selection() and Selection.get_selection_string. Anyhow I wonder whether there is some standardized way already available.

To get the translated string from a Selection field I use:

method = fields.Selection(METHODS, "Methods", ......)
method_string = method.translated('method')

See also Fields — Tryton server

Thx. But I’m seeking the other way round: Get the value from the translated string.

Aah, my bad. I don’t think there is a standard way for doing this. Sometimes you make the values of the Selection global so you can import them or use them in several classes. Or you are using the methods you already aware of.

In such case you will need to get all the tuple of translated values and read from there.
You can get the selection values from the field definition:

    pool = Pool()
    Model = pool.get('...')
    field_name = 'name_of_your_field'
    selection_values =  Model.fields_get([field_name])[field_name]['selection']

Selection values will be a list of tuples, first value is the value and the second the translated string.

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.