This question has been flagged
7 Replies
44471 Views

Hi !

I have a fields.selection, and I want to use the value of this field in a python function.

But, when I pass the field to this function, I get a string, the key...

So, how can I get the value instead ?

Thanks !

EDIT

Hi ! New week, already no answer for this... I wonder if a way to do this really exists...

Avatar
Discard
Best Answer
from odoo import api, models, _


def get_selection_label(self, object, field_name, field_value):
  return _(dict(self.env[object].fields_get(allfields=[field_name])[field_name]['selection'])[field_value])


class ResPartner(models.Model):
  _inherit = 'res.partner'

    def my_method(self):
        state_value_translated = get_selection_label(self, 'res.partner', 'state', self.state)
Avatar
Discard

Thank you! This has helped me to retrieve the selection field's label in Controller side as well.

Best Answer
  • model is the name of the model, example: 'account.invoice'
  • field is the name of the field inside the model, example: 'state'
  • field_val is the value of that field, example: 'draft'

Code:

dict(self.pool.get(model).fields_get(cr, uid, allfields=[field], context=context)[field]['selection'])[field_val]

Example:

dict(self.pool.get('account.invoice').fields_get(cr, uid, allfields=['state'], context=context)['state']['selection'])['draft']
Avatar
Discard
Author

Hum, this is quite unreadable, can you give me an example ?

Author

Well, thanks, I'll try this :)

Author

Works fine, thanks again !

test=dict(self.pool.get('sale.order').fields_get(cr, uid, allfields=['state'], context=context))['state']['selection']

    print "***************order",test                 ///////////////[('draft', u'Devis brouillon'), ('sent', u'Devis envoy\xe9'), ('cancel', u'Annul\xe9e'), ('waiting_date', u'Attente de planification'), ('progress', u'Bon de commande'), ('manual', u'Commande \xe0 facturer'), ('shipping_except', u"Incident d'exp\xe9dition"), ('invoice_except', u'Incident de facturation'), ('done', u'Termin\xe9e')]
Best Answer

The V8 way:

use the @api.one decorator

  • current model :value = dict(self.fields_get(allfields=['state'])['state']['selection'])['key']

  • other model : value = dict(self.env['your.model'].fields_get(allfields=['state'])['state']['selection'])['key']

Hope this help somebody..

Avatar
Discard
Best Answer

I use another technique.

First I define the list of pairs code/value

VALUES = [('a', 'A'), ('b','B'), ('c','C')]

I use it into my field definition

f = fields.Selection(VALUES)

and to return value, given the code, with a dict

dict(VALUES)[self.f]

or with a simple loop

for item in VALUES:
if item[0] == self.f:
return item[1]
Avatar
Discard

Super way ... especially for custom models

Best Answer

you can use this.

dict(self._fields['your_selection_field'].selection).get(self.your_selection_field)

Avatar
Discard

This gives me the Lable of the selection field. How to I get the value for the selected option

Best Answer

I use another technique.

First I define the list of pairs code/value

VALUES = [('a', 'A'), ('b','B'), ('c','C')]

I use it into my field definition

f = fields.Selection(VALUES)

and to return value, given the code, with a dict

dict(VALUES)[self.f]

or with a simple loop

for item in VALUES:
if item[0] == self.f:

Avatar
Discard

there must be some error: i can't explain myself this duplicate answer...