1

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.

Zoro
  • 47
  • 5

1 Answers1

1

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))
CZoellner
  • 13,553
  • 3
  • 25
  • 38