我正在一个向导中设置功能,它将执行以下操作:
在视图中,Customers_ids 是只读视图,其中 new_customers_ids 允许添加项目(客户)和删除。
当我从视图中添加新客户 (new_customers_ids) 但现在无法通过单击向导上的按钮(保存)更新 customers_ids(Customers ids) 时。如何通过在 (new_customers_ids) 中添加/删除和更新来添加/删除和更新 (customers_ids) 中的记录?
@api.multi
def applychanges(self):
for record in self:
customers = []
new_customers = []
for customer in record.customers_ids:
customers.append(customer.id)
customers = list(set(customers))
for x in record.new_customers_ids:
new_customers.append(x.id)
new_customers = list(set(new_customers_ids))
record.customers_ids = [(1, 0, new_customers)]
Run Code Online (Sandbox Code Playgroud)
我哪里做错了?
根据ORM 文档,使用左运算符的1应用作:
(1, id, 值)
而且它有效
id使用 values 中的值更新 id 的现有记录。
在您的代码中,您正在使用(1, 0, values)which 试图更新id == 0不可能存在的的记录。
就其价值而言,我很少看到左运算符用作1. 通常,我4用来更新记录值,这会将new_customer加到record的customer_ids字段中:
record.customers_ids = [(4, 0, new_customers[0])]
Run Code Online (Sandbox Code Playgroud)
但是 using4只支持一次添加一条记录(这就是我new_customers[0]在上面的例子中使用的原因。如果你想一次添加多条,可以使用列表推导:
record.customers_ids = [(4, 0, new_customer) for new_customer in new_customers]
Run Code Online (Sandbox Code Playgroud)
为了完整起见,这里是文档中的片段,其中包含每个可能的左运算符的用途和语法。作为参考,我几乎只使用过3, 4, 或6。
(0, _, 值)
添加从提供的值字典创建的新记录。
(1, id, 值)
id使用 中的值 更新 id 的现有记录values。不能用于create().(2, id, _)
id从集合中删除id 的记录,然后将其删除(从数据库中)。不能用于create().(3, id, _)
从集合中删除 id id 的记录,但不删除它。不能用于
One2many. 不能用于create().(4, id, _)
将 id 的现有记录添加
id到集合中。不能用于One2many.(5, _, _)
从集合中删除所有记录,相当于
3在每条记录上显式使用该命令。不能用于One2many. 不能用于create().(6, _, id)
用
ids列表替换集合中的所有现有记录,相当于使用命令5后跟4每个idin的命令ids。
| 归档时间: |
|
| 查看次数: |
11459 次 |
| 最近记录: |