Open digizilla-tohamy opened 4 months ago
I'm encountering an issue with installing hr_ modules in Odoo v17. Do you have any ideas why this exception is being raised?
From my humble debugging, I found that the issue is in hide_menu_user/models/res_users.py in class ResUsers:
def write(self, vals):
"""
Write method for the ResUsers model.
Ensure the menu will not remain hidden after removing it from the list.
"""
for menu in self.hide_menu_ids:
menu.restrict_user_ids = [fields.Command.link(self.id)]
res = super(ResUsers, self).write(vals)
return res
if you add a break point in this write method, when you install the addon,
the self
object will have multiple users, somthing like this
self= res.user(2,3,4,5)
Therefore, when the super write method res = super(ResUsers, self).write(vals)
is called
the expected singleton error is raised. as it expects only one user
My work arround was to to loop over each user and call the super write method for each one. So my write method now looks like this
def write(self, vals):
"""
Write method for the ResUsers model.
Ensure the menu will not remain hidden after removing it from the list.
"""
# Updated : looping over each record in self
for rec in self:
for menu in rec.hide_menu_ids:
menu.restrict_user_ids = [fields.Command.link(rec.id)]
res = super(ResUsers, rec).write(vals)
return res
This will dodge you the singleton error, and completes your addon installation. But I am not sure if this is the right way to do it, or if this causes any bugs.
I solved it by changing write method:
def write(self, vals):
"""
Write method for the ResUsers model.
Ensure the menu will not remain hidden after removing it from the list.
"""
res = super(ResUsers, self).write(vals)
for record in self:
for menu in record.hide_menu_ids:
menu.write({
'restrict_user_ids': [Command.set([record.id] + [user.id for user in menu.restrict_user_ids])]
})
return res
I'm also not sure which approach is best suitable in this case.
Thank you for sharing 🙏
hide_menu_user raises the following error when activating new addons the issue is in the overriden write method in ResUsers Class