【问题标题】:How to add , update and delete Many2many field records in odoo 10?如何在odoo 10中添加、更新和删除Many2many字段记录?
【发布时间】:2019-01-07 17:47:41
【问题描述】:

我正在一个向导中设置功能,它将执行以下操作:

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

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

当我从视图中添加新客户 (new_customers_ids) 但现在无法通过单击向导上的按钮(保存)来更新客户 ID(客户 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)]

我哪里做错了?

【问题讨论】:

    标签: python odoo


    【解决方案1】:

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

    (1, id, 值)

    而且很有效

    使用 values 中的值更新 id id 的现有记录。


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

    不管怎样,我很少看到左运算符用作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]
    

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

    (0,_,值)

    添加从提供的值字典创建的新记录。

    (1, id, values)

    使用中的值更新 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, _, ids)

    ids 列表替换集合中的所有现有记录,等效 对ids 中的每个id 使用命令5,后跟命令4

    【讨论】:

    • 面临这个问题 TypeError: unhashable type: 'list' On apply this code: @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 = [ (4、customers、new_customers)]
    • 使用4时,一次只能添加一个id。它给出了unhashable ype: list,因为它需要一个整数。您正在使用record.customers_ids = [(4, customers, new_customers)],其中customerslistnew_customers 是一个列表——这不起作用。添加一个customer_id 的正确用法如下:[(4, customer_id)] 其中customer_id 是一个整数。如果您想一次添加多个客户,您应该使用:record.customers_ids = [(4, new_customer_id) for new_customer_id in new_customers]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多