对于我的用例,我尝试在创建时将所有产品类别列表添加到联系人。
class product_category(models.Model):
_inherit = "product.category"
is_checked = fields.Boolean('Don', help="Check this box if this contact make Donation.")
resRelId = fields.Many2one('res.partner')
class res_partner(models.Model):
_inherit = "res.partner"
categs = fields.One2many('product.category','resRelId',String='Halo')
@api.model
def create(self, values):
categs = self.env['product.category'].search([])
new_id = super(res_partner, self).create(values)
for i in categs:
_logger.error(i)
new_id.write({'categs': i})
_logger.error(new_id.categs)
return new_id
我的代码编译没有错误,但我总是在联系人中找到一个空的产品列表,并且我收到了这些警告。
2016-07-04 17:54:46,777 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(1,) == 1 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:800)
2016-07-04 17:54:46,777 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(1,) == 2 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:802)
2016-07-04 17:54:46,777 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(1,) == 3 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:804)
2016-07-04 17:54:46,777 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(1,) == 4 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:812)
2016-07-04 17:54:46,777 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(1,) == 5 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:818)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(1,) == 6 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:831)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(3,) == 1 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:800)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(3,) == 2 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:802)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(3,) == 3 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:804)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(3,) == 4 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:812)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(3,) == 5 (/home/odoo/odoo-dev/odoo/openerp/osv/fields.py:818)
2016-07-04 17:54:46,778 17751 WARNING v8dev openerp.models: Comparing apples and oranges: product.category(3,) == 6 (/home/odoo/odoo-
答案 0 :(得分:0)
您正在尝试写入one2many字段。那不是你写给One2many的方式。
这是一个从one2many添加和删除的各种方法的简单提示:
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write *values* on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5) unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
在您的情况下执行此操作的正确方法是:
def create(self, values):
categs = self.env['product.category'].search([])
new_id = super(res_partner, self).create(values)
for i in categs:
_logger.error(i)
new_id.write({'categs': (4,i.id)})
_logger.error(new_id.categs)
return new_id
答案 1 :(得分:0)
有一种最简单的方法可以做到这一点。
@api.model
def create(self, values):
## Here categ is a list containing category ids.
categs = self.env['product.category'].search([]).ids
values.update({'categs':[(6, 0, categs or [])]})
return super(res_partner, self).create(values)