我想用从 MySQL select 语句检索的值填充 Odoo 中的 fields.Selection。 我尝试在数组中添加 select resultset 的值,并将该数组返回到目标字段。但是我没有好的结果,当我调用 onchange_Model 函数时,我收到一个“list”对象没有属性“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"/>
还有其他方法吗?
最佳答案
更改选择值是不可能的(因为选择值对于您使用它们的所有模型都是静态和通用的)。
一个好的方法是创建一个新模型并使用域过滤器进行过滤
关于python - 使用 Odoo 从 Mysql 填充选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46429392/