跳至內容
選單
此問題已被標幟
7 回覆
47286 瀏覽次數

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...

頭像
捨棄
最佳答案
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)
頭像
捨棄

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

最佳答案
  • 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']
頭像
捨棄
作者

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

作者

Well, thanks, I'll try this :)

作者

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')]
最佳答案

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]
頭像
捨棄

Super way ... especially for custom models

最佳答案

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..

頭像
捨棄
最佳答案

you can use this.

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

頭像
捨棄

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

最佳答案

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:

頭像
捨棄

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

相關帖文 回覆 瀏覽次數 活動
9
4月 22
28800
1
3月 15
3678
0
5月 20
6161
2
12月 17
16645
1
6月 16
11178