Search code examples
many-to-manyodooone-to-many

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


Since yesterday I'm facing a weird problem.I'm trying to add all the product category list to a contact at creation time.It's not working neither for a one2many/many2one relation or many2many/many2many relation. I always end up with an empty list of categories in the contact.

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()

Solution

  • Your create() should look like this:

    @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
    

    That code example is for a Many2Many field (categs), which i would prefer here. For Many2Many you need to use a List of tuples. You can find the possible tuples here.