添加到 Many2many 字段的用户在单击保存后消失 [英] Added users to Many2many field disappear after click save
问题描述
我尝试将用户添加到 many2many 字段.在点击保存之前它正在工作.当我点击保存它消失了.但是如果我在添加一些额外的元素之前做这个字段,其他元素在点击保存后保留.
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.
代码:
@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]]
屏幕:
点击保存前的一个元素:
One element before click save:
点击保存后:
点击保存前手动添加额外的元素:
Before click save add manually extra element:
点击保存并手动添加一个元素后:
After click save with one manually added element:
推荐答案
在onchange
中,当你想更新的值时不要使用
,使用command list
many2manyRecordSet
,我会稍微简化一下你的代码:
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
当我调查传递给 create 和 write 的字典值时,Odoo 正在将命令转换为仅 update
命令,在我通过带有 id 1,2
字典里的命令是这样的!!:
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'}]]
在新版本的Odoo (> 11.0)
中many2many
传递的命令是替换命令:[(6, 0, ids)]代码> <代码>(错误已修复)代码>:
In new version of Odoo (> 11.0)
the commands passed by many2many
are replace commands: [(6, 0, ids)]
(bug was fixed)
:
并且要在您的情况下解决此问题,只需覆盖 create a 并写入以修复您的字段的命令:
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'}]]
注意:我注意到,当您手动添加记录时,命令 (6,0, ids)
被传递,问题不会出现,为什么我检查了命令在修复它们之前存在
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
这篇关于添加到 Many2many 字段的用户在单击保存后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!