从昨天开始,我面临一个奇怪的问题。我试图在创建时将所有产品类别列表添加到联系人中。它既不适用于one2many / many2one关系,也不适用于many2many / many2many关系。我总是在联系人中找到一个空列表。
class product_category(models.Model):
_inherit = "product.category"
contacts = fields.Many2many('res.partner')
class odepoContact(models.Model):
_inherit = "res.partner"
categs = fields.Many2many('product.category')
@api.model
def create(self, values):
## Here categ is a list containing category ids.
categs = self.env['product.category'].search([])
# values['categs'] = (4,0,categs) Not working =>EMPTY
# values['categs'] = categs Not working =>EMPTY
# values['categs'] = (6,0,categs) Not working =>EMPTY
id = super(odepoContact, self).create(values)
_logger.error(id.categs)
return id
LOG:v8dev openerp.addons.org_chart_dept.org_chart: product.category()
答案 0 :(得分:1)
你的create()应如下所示:
@api.model
def create(self, values):
categs = self.env['product.category'].search([])
values['categs'] = [(6,0,categs.ids)]
id = super(odepoContact, self).create(values)
_logger.error(id.categs)
return id
该代码示例适用于Many2Many字段(categs),我更喜欢这里。对于Many2Many,您需要使用元组列表。您可以找到可能的元组here。