尝试在 odoo v8 中填充 Many2many 或 One2many 列表时出现错误

Bug when trying to fill Many2many or One2many list in odoo v8

从昨天开始,我遇到了一个奇怪的问题 problem.I 我试图在创建时将所有产品类别列表添加到联系人 time.It 对 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()

您的 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.