获取选择字段的值而不是键
Fetch value of selection field instead of key
我在一个模型中定义了一个 selection
字段。
type = fields.Selection([('a','A'),('b','B'),('c','C')])
在其中一个函数中,我试图得到 string value
而不是 key
。
@api.multi
def testFunc(self):
for res in self:
print'Value',res.type //It prints 'a'.
我需要打印 'A'。
我该怎么做?
选择一种解决方案:
最重要的是,您可以获得这样的选择列表:
self._fields['type'].selection
所以试试这个:
# convert the list to dictionary
dict(self._fields['type'].selection).get(self.type)
如果您希望将标签翻译成用户语言:
# here the label return is translated.
value = dict(self.fields['state']._description_selection(self.evn)).get(self.type)
你可以使用这个方法,它returns字符串值,如果是的话翻译:
@api.multi
def testFunc(self):
for res in self:
print'Value', dict(res.fields_get(["type"],['selection'])['type']["selection"]).get(res.type)
一个可能的简单解决方案是:
VALUES_TYPE = [('a','A'),('b','B'),('c','C')]
type = fields.Selection(VALUES_TYPE )
dict(VALUES_TYPE )[self.type]
我在一个模型中定义了一个 selection
字段。
type = fields.Selection([('a','A'),('b','B'),('c','C')])
在其中一个函数中,我试图得到 string value
而不是 key
。
@api.multi
def testFunc(self):
for res in self:
print'Value',res.type //It prints 'a'.
我需要打印 'A'。
我该怎么做?
选择一种解决方案:
最重要的是,您可以获得这样的选择列表:
self._fields['type'].selection
所以试试这个:
# convert the list to dictionary
dict(self._fields['type'].selection).get(self.type)
如果您希望将标签翻译成用户语言:
# here the label return is translated.
value = dict(self.fields['state']._description_selection(self.evn)).get(self.type)
你可以使用这个方法,它returns字符串值,如果是的话翻译:
@api.multi
def testFunc(self):
for res in self:
print'Value', dict(res.fields_get(["type"],['selection'])['type']["selection"]).get(res.type)
一个可能的简单解决方案是:
VALUES_TYPE = [('a','A'),('b','B'),('c','C')]
type = fields.Selection(VALUES_TYPE )
dict(VALUES_TYPE )[self.type]