简体   繁体   English

如何获取选择字段odoo 15的选定值

[英]How to get selected value of selection field odoo 15

i have this selection field我有这个选择字段

Fam = fields.Selection(selection=[('x1','x1'),('x2','x2')])

i want to get the selected value of Fam selection field and write the condition if no item is selected in Fam我想获取 Fam 选择字段的选定值,并在 Fam 中未选择任何项目时写入条件

i tried this code我试过这段代码

@api.onchange('Fam')
def _onchange_Fam(self):
    #here i want to write condition of if No item is selected in Fam selection field
    if(self.Fam == ''):
        #code
    else:
        #print the selected value
        print (dict(self._fields['Fam'].selection).get(self.Fam))

But i get this error:但我得到这个错误:

ValueError: dictionary update sequence element #0 has length 1; ValueError:字典更新序列元素#0 的长度为 1; 2 is required 2 是必需的

i want to know how to print selected field and how to check if there is a selected item in the selection field我想知道如何打印选定的字段以及如何检查选择字段中是否有选定的项目

thanks.谢谢。

Your check should work already, but it is enough to check if the field value is Falsy .您的检查应该已经有效,但足以检查字段值是否为Falsy

If you want to have the selection value (not the key) you could use _description_selection() of that field, which will also handle translation of that value:如果你想要选择值(不是键),你可以使用该字段的_description_selection() ,它也将处理该值的翻译:

@api.onchange('Fam')
def _onchange_Fam(self):
    if not self.Fam:
        # some code
    else:
        #print the selected value
        print (dict(self._fields['Fam']._description_selection(self.env)).get(self.Fam))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM