使用 Odoo 从 Mysql 填充选择字段

Populate selection field from Mysql with Odoo

我想用从 MySQL select 语句中检索到的值填充 Odoo 中的 fields.Selection。 我尝试在数组中添加 select 结果集的值,并将该数组返回到目标字段。但是我没有什么好的结果,当我调用 onchange_Model 函数时收到 'list' object has no attribute 'get' 错误。 这是我的 Python 代码:

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 行对应 'Marca' 和 'Model' 字段:

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

还有其他方法吗?

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

一个好的方法是创建一个新模型并使用域过滤器进行过滤