2017-09-26 97 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

更改選擇值從更改是不可能的(因爲選擇值對於您使用它們的所有模型都是靜態的和通用的)。

一個好的方法將創建一個新的模型,並用域濾波