Search code examples
pythonxmlodooodoo-14odoo-15

How to add query returned value to selection field in odoo 15


i want to add this returned list from get_famille_list function to selection field in odoo-15

get_famille_list function :

def get_famille_list(self):
    all_fam = []
    query = """ SELECT x FROM product_template WHERE x !='' """
    self.env.cr.execute(query)
    data = self.env.cr.fetchall()
    for fam in data:
        all_fam.append(fam[0])
    return all_fam

And this is how i link the selection with the returned list from get_famille_list function

famille = fields.Selection(selection='get_famille_list')

expected data (selection= x1,x2,x3)

insteed of showing the correct data in the selection field it shows something weird, see the image

enter image description here

i can't figure out why it shows this. any help will be appreciated, Thanks


Solution

  • You need to return a list of pairs (value, label).

    You just need to append a tuple to the all_fam list

    Example:

    def get_famille_list(self):
        all_fam = []
        query = """ SELECT x FROM product_template WHERE x !='' """
        self.env.cr.execute(query)
        data = self.env.cr.fetchall()
        for fam in data:
            all_fam.append((fam[0], fam[0].capitalize()))
        return all_fam