Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N+1 Query #799

Open
aropan opened this issue Aug 5, 2023 · 0 comments
Open

N+1 Query #799

aropan opened this issue Aug 5, 2023 · 0 comments

Comments

@aropan
Copy link

aropan commented Aug 5, 2023

In Django 4.2, I encountered an "N+1 query" issue when using {% perms... %} in templates. The problem was caused by the anonymous user, though it worked fine for regular users:

if not user_obj.is_authenticated:
# If anonymous user permission is disabled then they are always
# unauthorized
if settings.ANONYMOUS_USER_NAME is None:
return False, user_obj
User = get_user_model()
lookup = {User.USERNAME_FIELD: settings.ANONYMOUS_USER_NAME}
user_obj = User.objects.get(**lookup)

I resolved this by setting ANONYMOUS_USER_NAME to None. More details can be found in the Django Guardian documentation: https://django-guardian.readthedocs.io/en/stable/configuration.html#anonymous-user-name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant