3

I defined a selection field in one model.

type = fields.Selection([('a','A'),('b','B'),('c','C')])

In one of function i tried to get string value instead of key.

@api.multi
    def testFunc(self):
    for res in self:
        print'Value',res.type //It prints 'a'.

I need to print 'A'.

How can i do this?

KbiR
  • 4,047
  • 6
  • 37
  • 103

3 Answers3

4

Choose One of the solutions :

The most importing thing that you can get the selection list like this:

    self._fields['type'].selection

So try this:

    # convert the list to dictionary
    dict(self._fields['type'].selection).get(self.type)

IF you want the label to be translated in user language:

  # here the label return is translated.
  value = dict(self.fields['state']._description_selection(self.evn)).get(self.type)
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
0

You can use this method, it returns the string value, translated if it's the case:

@api.multi
    def testFunc(self):
    for res in self:
        print'Value', dict(res.fields_get(["type"],['selection'])['type']["selection"]).get(res.type)
Beto
  • 11
  • 4
0

A possible and simple solution would be:

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

type = fields.Selection(VALUES_TYPE )

dict(VALUES_TYPE )[self.type]