Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
6 Trả lời
27123 Lượt xem

Is there any method to fetch the label of a selection field from its value?

For eg:-

'type': fields.selection([('product','Stockable Product'),('service','Service Product'), ('consu','Consumable Product')],'Type')

if we wish to fetch a product type like product.type if its value is 'service' we want to get its label as 'Service Product', is there any way to get the label in python or in openerp ?

Ảnh đại diện
Huỷ bỏ

One solution for this is to make a dictionary like { 'product':'Stockable Product','service':'Service Product', 'consu':'Consumable Product'}. According to the conditions we can fetch the key values from the dictionary

Tác giả

Ok thanks. But is there any method to fetch it directly from the field description itself?

Câu trả lời hay nhất

Hello Abhishek,

Very simple code that will help you:

# First fetch the dictionary with key-value pair that was defined in your field
kay_val_dict = dict(self._fields['type'].selection) # here 'type' is field name

# Now iterate loop for all pair of key-val and based on that you can set label
for key, val in kay_val_dict.items():
    if key == 'service':
        # Do your code
        Label = value
        print("LABEL = ", Label)



I hope it will helpful for you

Thanks and regards
Haresh Kansara


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Abhishek,

You can try this with your object and field 'type':

dict(self.pool.get('YOUR.OBJECT').fields_get(cr, uid, allfields=['FIELD'], context=context)['FIELD]['selection'])['KEY']

Hope it helps!    


Ảnh đại diện
Huỷ bỏ

thanks, works well :)

Câu trả lời hay nhất

this self.fields_get(cr, uid, ["field"], context=context) Return the field definition. I hope help you.

Ảnh đại diện
Huỷ bỏ