gpt4 book ai didi

python - 如何在 odoo 10 中添加、更新和删除 Many2many 字段记录?

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:49 30 4
gpt4 key购买 nike

我正在向导中设置函数,它将执行以下操作:

  1. 添加新记录并链接到当前现有的 Many2many 字段。
  2. 更新当前存在的 Many2many 字段的记录。
  3. 删除当前存在的 Many2many 字段。向导模型和实际模型有两个 Many2many 字段
  4. customers_ids = fields.Many2many('res.partners', 'Customers')
  5. new_customers_ids = fields.Many2many('res.partners', '新客户')

在 View 中,Customers_ids 是只读 View ,其中 new_customers_ids 允许添加项目(客户)和删除。

当我从 View 中添加新客户 new_customers_ids 但现在无法通过单击向导上的 save 按钮更新 customers_ids(客户 ID)时.如何通过在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)]

最佳答案

根据 the ORM documentation ,使用 1 的左运算符应该用作:

(1, id, values)

而且效果很好

Updates an existing record of id id with the values in values.


在您的代码中,您正在使用 (1, 0, values) 尝试更新 id == 0 的记录,该记录不可能存在.

就其值(value)而言,我很少看到左运算符用作 1。通常,我使用 4 更新记录值,这会将 new_customer 添加到 recordcustomer_ids领域:

record.customers_ids = [(4, 0, new_customers[0])]

但是,使用4只支持一次添加一条记录(这就是我在上面的例子中使用new_customers[0]的原因。如果你想添加很多立即,您可以使用列表理解:

record.customers_ids = [(4, 0, new_customer) for new_customer in new_customers]

为了完整起见,这里是文档中的片段,其中包含每个可能的左运算符的用途和语法。作为引用,我几乎只使用过 346

(0, _, values)

Adds a new record created from the provided value dict.

(1, id, values)

Updates an existing record of id id with the values in values. Can not be used in create().

(2, id, _)

Removes the record of id id from the set, then deletes it (from the database). Can not be used in create().

(3, id, _)

Removes the record of id id from the set, but does not delete it. Can not be used on One2many. Can not be used in create().

(4, id, _)

Adds an existing record of id id to the set. Can not be used on One2many.

(5, _, _)

Removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used on One2many. Can not be used in create().

(6, _, ids)

Replaces all existing records in the set by the ids list, equivalent to using the command 5 followed by a command 4 for each id in ids.

关于python - 如何在 odoo 10 中添加、更新和删除 Many2many 字段记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54079315/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com