콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
5 답글
25510 화면

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?


아바타
취소
베스트 답변

you can use this.

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

아바타
취소
베스트 답변

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

아바타
취소

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

Thank you, Mayank sir +1

베스트 답변

 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

아바타
취소
베스트 답변
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)
아바타
취소
베스트 답변
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
아바타
취소
관련 게시물 답글 화면 활동
0
2월 18
4938
2
9월 23
12410
3
6월 19
6555
1
7월 17
4919
2
7월 25
4574