3
votes

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
Doesnt look bad, did you try to log modellist before the return? it should be a list of tuples, maybe you dont have any res?. Does it breaks only in the on change or when updating the module too? - dccdany
@dccdany yes, I receive a list of tuples with the results, but after I receive the error - SirGuacamole
Does it breaks only in the on change or when updating the module too? - dccdany
@dccdany only in the on change - SirGuacamole
im afraid i dont think you can do it from an on change method :/. You should try with a many2one to a new model and setting the marca there, then with a domain filter you can restrict to show only what you want to - dccdany

1 Answers

1
votes

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