Added users to Many2many field disappear after click save

久未见 提交于 2021-01-29 04:10:28

问题


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 element stay after clicking save.

Code:

@api.multi
@api.onchange('partner_id')
def find_projects(self):
    mail_followers = self.env["mail.followers"]
    projects_follower_id = mail_followers.search([('partner_id', '=', self.partner_id.id), ('res_model', '=', 'project.project')])
    projects_list = []
    for x in range(len(projects_follower_id)):
        project_id = self.env["project.project"].search([('id', '=', projects_follower_id[x].res_id), ('active', '=', True)])
        if project_id and project_id not in projects_list:
            projects_list.append(project_id.id)

    self.debug_projects = len(projects_follower_id)
    self.debug_projects2 = projects_follower_id
    self.debug_projects3 = projects_list
    self.project_ids = [[6, 0, projects_list]]

Screens:

One element before click save:

After click save:

Before click save add manually extra element:

After click save with one manually added element:


回答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.




回答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



来源:https://stackoverflow.com/questions/58283436/added-users-to-many2many-field-disappear-after-click-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!