2017-09-26 101 views
3

我想使用从MySQL select语句检索的值填充Odoo中的fields.Selection。 我尝试在数组内部添加select结果集的值,并将此数组返回到目标字段。但是我没有好的结果,当我调用onchange_Model函数时,我收到一个'list'对象没有属性'get'错误。 这是我的Python代码:使用Odoo从Mysql填充选择字段

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') 

以及对应于“马卡报”我的XML线和“模式”字段:

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

有没有其他办法可以做到这一点?

+1

犯规过滤看起来很糟糕,你尝试恢复之前登录modellist?它应该是一个元组列表,也许你没有任何资源?只有在更改或更新模块时才会中断吗? – dccdany

+0

@dccdany是的,我收到了包含结果的元组列表,但是在收到错误 – SirGuacamole

+0

之后,它是否仅在更改或更新模块时中断? – dccdany

回答

1

更改选择值从更改是不可能的(因为选择值对于您使用它们的所有模型都是静态的和通用的)。

一个好的方法将创建一个新的模型,并用域滤波