如何在odoo 10中添加,更新和删除Many2many字段记录? [英] How to add , update and delete Many2many field records in odoo 10?

查看:1053
本文介绍了如何在odoo 10中添加,更新和删除Many2many字段记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am setting up function in a wizard which will do following actions:

  1. 添加新记录并链接到当前现有的Many2many字段.
  2. 更新当前现有的Many2many字段的记录.
  3. 删除当前现有的Many2many字段. 向导模型和实际模型具有两个Many2many字段
  4. customers_ids = fields.Many2many('res.partners','Customers')
  5. new_customers_ids = fields.Many2many('res.partners','新客户')
  1. Add new record and linked to current existing Many2many Field.
  2. Update record of current existing Many2many Field.
  3. Delete current existing Many2many Field. Wizard model and Actual model having two Many2many fields
  4. customers_ids = fields.Many2many('res.partners', 'Customers')
  5. new_customers_ids = fields.Many2many('res.partners', 'New Customers')

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

In View Customers_ids are read only view where as new_customers_ids allowing add item (customer) and delete.

当我从视图中添加新客户(new_customers_ids)但现在无法通过单击向导上的按钮(保存)来更新customer_ids(客户ID)时. 如何通过(new_customers_ids)中的添加/删除和更新来从(customers_ids)中添加/删除和更新记录?

When i am adding new customers (new_customers_ids) from view but can't now updating customers_ids(Customers ids) via clicking button (save) on wizard. How can add/ delete and update records from (customers_ids) via adding/ deleting and updating in (new_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)]

我在哪里做错了?

推荐答案

每个

Per the ORM documentation, using a left operator of 1 should be used as:

(1,id,values)

(1, id, values)

有效

使用值中的值更新ID为id的现有记录.


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


In your code, you are using (1, 0, values) which is trying to update the record of id == 0, which can't possibly exist.

对于它的价值,我很少见到左运算符用作1.通常,我使用4更新记录值,这会将new_customer添加到recordcustomer_ids字段中:

For what it's worth, I've rarely seen the left operator used as 1. Usually, I use 4 to update the record value, which will add the new_customer to the record's customer_ids field:

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

但是,使用4仅支持一次添加一条记录(这就是我在上面的示例中使用new_customers[0]的原因.如果要一次添加许多记录,则可以使用列表理解:

However, using 4 only supports adding one record at a time (which is why I used new_customers[0] in the above example. If you want to add many at once, you can use list comprehension:

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


为完整起见,这是文档中的代码片段,其中包含每个可能的left运算符的用途和语法.作为参考,我几乎只使用过346.


For completeness, here is the snippet from the documentation with the purpose and syntax of each possible left operator. For reference, I've almost only ever used 3, 4, or 6.

(0,_,值)

添加根据提供的值字典创建的新记录.

Adds a new record created from the provided value dict.

(1,ID,值)

使用ID中的值更新ID为id的现有记录 values.不能在create()中使用.

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

(2,ID,_)

从集中删除ID id的记录,然后从数据库中删除它.不能在create()中使用.

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

(3个ID,_)

从集中删除id id的记录,但不删除它.不能在One2many上使用.不能在create()中使用.

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,_)

将ID为id的现有记录添加到集合中.不能在One2many上使用.

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

(5,_,_)

从集合中删除所有记录,等效于在每个记录上显式使用命令3.不能在One2many上使用.不能在create()中使用.

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个,_,id)

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

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.

这篇关于如何在odoo 10中添加,更新和删除Many2many字段记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆