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?