用Od填充Mysql中的选择字段

2024-09-27 00:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想填充一个字段。选择在Odoo中使用从MySQL select语句检索的值。 我尝试在数组中添加selectresultset的值,并将这个数组返回到目标字段。但是我没有得到好的结果,当我调用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')

以及对应于“Marca”和“Model”字段的XML行:

^{pr2}$

有别的办法吗?在


Tags: selffieldsmodelres数组cursorrowmysqldb
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:55

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

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

相关问题 更多 >

    热门问题