Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odgovori
1341 Prikazi

I am trying to add record rule:



class ResPartner(models.Model):
_inherit = 'res.partner'

@api.model
def _search(self, args, offset=0, limit=None, order=None, access_rights_uid=None):

if not self.env.user.has_group('hat_sales_updates.part_restrict_group'):
args = [('team_id', '=', self.env.user.sale_team_id.id)] + args
else:
args = [(1, '=', 1)] + args

return super(ResPartner, self)._search(args, offset=offset, limit=limit, order=order,
access_rights_uid=access_rights_uid)

but the error ocures:
maximum recursion depth exceeded while calling a Python object
in odoo17
in xml user access error occures:



Avatar
Opusti
Best Answer

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.

Avatar
Opusti
Best Answer

Hi Asma,
Did you find any solution for it?

Avatar
Opusti
Best Answer

I think u should use xml ir.rule only

You need to add another record rule for hat_sales_updates.admin (if you have any) with domain_force 1=1 too this way will help Michell Admin can accessible

Avatar
Opusti