Skip to content

Commit

Permalink
fix(account): allow User.objects to use prefetch_related()
Browse files Browse the repository at this point in the history
Django forbids QuerySet.iterator(chunk_size=None) on QuerySets that have QuerySet.prefetch_related(), some chunk_size must be specified. 2000 is the default chunk_size for when there is no prefetching.

In my case, I am using custom UserManager for User.objects and I do use prefetch. Specifically I join price list information (including currency and VAT rate) for the user since that's used practically everywhere on the site and I want to avoid another unnecessary query as well as de-normalizing this information to all users.

Reviewed-on: https://codeberg.org/allauth/django-allauth/pulls/4196
Co-authored-by: Jan Hamal Dvořák <[email protected]>
Co-committed-by: Jan Hamal Dvořák <[email protected]>
  • Loading branch information
mordae authored and pennersr committed Dec 11, 2024
1 parent 45ea40a commit b8e25d5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def unset_extra_primary_emails(user):
break
qs.exclude(pk=primary_email_address.pk).update(primary=False)

for user in get_users_with_multiple_primary_email().iterator():
for user in get_users_with_multiple_primary_email().iterator(2000):
unset_extra_primary_emails(user)


Expand Down
2 changes: 1 addition & 1 deletion allauth/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def filter_users_by_email(
if app_settings.USER_MODEL_EMAIL_FIELD and not is_verified:
q_dict = {app_settings.USER_MODEL_EMAIL_FIELD: email}
user_qs = User.objects.filter(**q_dict)
for user in user_qs.iterator():
for user in user_qs.iterator(2000):
user_email = getattr(user, app_settings.USER_MODEL_EMAIL_FIELD)
if _unicode_ci_compare(user_email, email):
users.append(user)
Expand Down

0 comments on commit b8e25d5

Please sign in to comment.