Populate selection field from Mysql with Odoo

Viewed 624

I want to populate a fields.Selection in Odoo with values retrieved from MySQL select statement. I tried adding the values of select resultset inside of an array, and returning this array to the destination field. But I have no good results, I receive an 'list' object has no attribute 'get' error when I call the onchange_Model function. This is my Python code:

mydb = MySQLdb.connect('host','user','pass','DB',cursorclass=MySQLdb.cursors.DictCursor)
cursor=mydb.cursor()

    marca = fields.Selection([('a','A'),('b','B')])

                @api.onchange('marca')
                def onchange_Model(self):

                        modellist=[]

                        q = self.cursor.execute("SELECT field1,field2 FROM Table WHERE field1 LIKE '%s' GROUP BY field1"%(self.marca))
                        res = self.cursor.fetchall()
                        for row in res:
                            modellist.append((str(row["field1"]),str(row["field2"])))
                        return modellist

    models = fields.Selection(selection='onchange_Model')

And my XML lines corresponding to 'Marca' and 'Model' fields:

<field name="marca" widget="selection" on_change="1"/>
<field name="model" widget="selection" on_change="1"/>

Is there other way to do this?

1 Answers

Changing the selection value from on change is impossible (because selection values are static and generic for all the models that you use them).

A good approach would be creating a new model and filtering with a domain filter

Related