1. XML Access Rule:
The XML definition you provided for restricting partner access seems correct, but you might need to double-check a few things:
- Make sure the group hat_sales_updates.part_restrict_group is properly assigned to the intended users.
- Ensure that the field team_id exists in the res.partner model and is correctly populated.
2. Maximum Recursion Depth Issue:
The recursion error usually occurs because the _search method is being called repeatedly without an exit condition. This may be happening because your method is causing an infinite loop. The _search method might be invoking itself indirectly.
Here's a refined version of your _search method that should help avoid recursion:
class ResPartner(models.Model):
_inherit = 'res.partner'
@api.model
def _search(self, args, offset=0, limit=None, order=None, access_rights_uid=None):
# Avoid recursion by checking the environment
if self.env.context.get('prevent_recursion', False):
return super(ResPartner, self)._search(args, offset=offset, limit=limit, order=order,
access_rights_uid=access_rights_uid)
if not self.env.user.has_group('hat_sales_updates.part_restrict_group'):
args = [('team_id', '=', self.env.user.sale_team_id.id)] + args
# Set a context key to prevent recursion
ctx = dict(self.env.context, prevent_recursion=True)
return super(ResPartner, self.with_context(ctx))._search(args, offset=offset, limit=limit, order=order,
access_rights_uid=access_rights_uid)
return super(ResPartner, self)._search(args, offset=offset, limit=limit, order=order, access_rights_uid=access_rights_uid)
The line self.env.context.get('prevent_recursion', False) checks if recursion prevention is already in place.
- self.with_context(ctx) sets a context variable prevent_recursion to avoid further recursion calls.
You might also need to update your access rules. Make sure you have a record rule like this:
Partner restricted to sales team
[('team_id', '=', user.sale_team_id.id)]
Try these adjustments, and restart your Odoo server to apply the changes.