Skip to content Skip to sidebar Skip to footer

Added Users To Many2many Field Disappear After Click Save

I try to add users to many2many field. It's working to moment before clicking save. When i click save it's disappear. But if i before add some extra element do this field, other el

Solution 1:

The problem is this bug:

https://github.com/odoo/odoo/issues/14761

In default view it not working, when i add widget="many2many_tags" in view it start working.


Solution 2:

In onchange don't use command list when you want to update the value of many2many, Use RecordSet, I'm going to simplify your code a little bit:

# don't use api.multi because it's by default multi with onchange, constraints, depends
@api.onchange('partner_id')
def find_projects(self):
    # extract followers <RecordsSet>
    projects_followers = self.env["mail.followers"].search([('partner_id', '=', self.partner_id.id), ('res_model', '=', 'project.project')])
    # extract all project ids without duplication <list of int >
    project_ids = projects_followers.mapped('res_id')
    # no need to pass active = True Odoo by default add it
    # search for porject <RecordSet>
    projects = self.env["project.project"]search([('id', 'in', project_ids)])
    # for debuging
    self.debug_projects = len(projects_followers)
    self.debug_projects2 = projects_followers
    self.debug_projects3 = project_ids

    # don't use any command by default Odoo detect witch project still in the field
    # and witch one are added when you inspect write you will find that new ones are added 
    # by (4, id)  the ones that were removed are (3, id)
    self.project_ids = projects

Edits:

When I investigated the dictionary values passed to create and write, Odoo was converting the commands to only update commands, after I added by onchange event records with ids 1,2 the commands in the dictionary were like this!!:

      'my_many2may' : [[1, 1, {u'name': u'1'}], [1, 2, {u'name': u'2'}]]

In new version of Odoo (> 11.0) the commands passed by many2many are replace commands: [(6, 0, ids)] (bug was fixed):

And to fix this in your case just override the create aand write to fix the commads of your field:

def _fix_vals(self, vals):
    """ fix bug of ignored record added by onchange event."""
    commands = vals.get('project_ids', [])
    if commands and not any(command[0] == 6 for command in commands):
        vals['project_ids'] +=  [(4, command[1]) for command in commands if command[0] == 1]


@api.model
def create(self, vals):
    self._fix_vals(vals)
    return super(YourClassName, self).create(vals)


@api.multi
def write(self, vals):
    self._fix_vals(vals)
    return super(YourClassName, self).write(vals)

so basically when I fix the commonds like this:

    [[1, 1, {u'name': u'1'}], [1, 2, {u'name': u'2'}]]
    # to this
    [(4,1), (4,2), [1, 1, {u'name': u'1'}], [1, 2, {u'name': u'2'}]]

Note: I noticed that when you add a record manually the command (6,0, ids) is passed the problem will not appear this why I checked if that command exist before fixing them


Post a Comment for "Added Users To Many2many Field Disappear After Click Save"