简体   繁体   中英

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

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; 2 is required

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 .

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:

@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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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