Help

0

How to get key value list from selection fields in Odoo 10

Avatar
parag

I have a selection field, and I want to fetch list of all keys and values using ORM method. How can I achieve it.


    _name = "calling_devices"

    device_type = fields.Selection([('desk','Desk'),('mobile','Mobile'),('home','Home'),('others','Others')])


Desired Output using ORM methods or alternate way:

Key_Value_pair = {'desk': 'Desk', ' mobile ' : ' Mobile ', ' home ': ' Home ', ' others ': ' Others '}

How can we achieve it?


Avatar
Discard
4 Answers
0
Avatar
Pankaj Dhedhi
Best Answer

you can use this.

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

Avatar
Discard
2
Avatar
Mayank Gosai
Best Answer

Hi Parag,

You can get your selection by:

self._fields['your_selection_field'].selection

In your case:

var = self._fields['device_type'].selection

output: [('desk','Desk'),('mobile','Mobile'),('home','Home'),('others','Others')]

Then convert this into Dictionary.

dict(var)

Final Output: {'desk': 'Desk', ' mobile ' : ' Mobile ', ' home ': ' Home ', ' others ': ' Others '}


Hope it helps!

Regards,

Mayank Gosai

2 Comments
Avatar
Discard
Avatar
Thanh Loyal
-

I didn't know about this. Thank you for your information.

Avatar
Ankit H Gandhi(AHG)
-

Thank you, Mayank sir +1

0
Avatar
tuyenvn
Best Answer

 have followed Mayank Gosai's instructions, this is a good way.

var = self._fields ['device_type']. selection

dict_var = dict (var)

And now I can know the current value of the device_type field of the current record

record_device_type = dict_var [self.att_type_dwg]

such as if self.device_type = 'home' then record_device_type = 'Home'.
:)

Thanks Mayank Gosai

Avatar
Discard
0
Avatar
Nebil Aydi
Best Answer
def _get_key_value_list(self):
    Key_Value_pair={}
    for elmt in self.device_type:
        Key_Value_pair.update({elmt[0]: elmt[1]})
    return Key_Value_pair
Avatar
Discard