Skip to content

Commit 08e8366

Browse files
authored
Replace wrong usage of prefetch_related with select_related (#12602)
prefetch_related should be used for many-to-many and one-to-many relationships, while select_related is for foreign key and one-to-one relationships. Otherwise, we are doing an extra query instead of saving one.
1 parent 5790f40 commit 08e8366

File tree

4 files changed

+4
-4
lines changed

4 files changed

+4
-4
lines changed

readthedocs/organizations/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_team_queryset(self):
4141
return Team.objects.member(
4242
self.request.user,
4343
organization=self.organization,
44-
).prefetch_related("organization")
44+
).select_related("organization")
4545

4646

4747
class OrganizationSortOrderingFilter(OrderingFilter):

readthedocs/organizations/migrations/0008_migrate_old_invitations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def forwards_func(apps, schema_editor):
1919
Invitation = apps.get_model("invitations", "Invitation")
2020
ContentType = apps.get_model("contenttypes", "ContentType")
2121

22-
queryset = TeamInvite.objects.filter(teammember__member__isnull=True).prefetch_related(
22+
queryset = TeamInvite.objects.filter(teammember__member__isnull=True).select_related(
2323
"organization"
2424
)
2525
for team_invite in queryset:

readthedocs/organizations/views/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class OrganizationOwnerView(OrganizationMixin):
172172
def get_queryset(self):
173173
return OrganizationOwner.objects.filter(
174174
organization=self.get_organization(),
175-
).prefetch_related("owner")
175+
).select_related("owner")
176176

177177
def get_form(self, data=None, files=None, **kwargs):
178178
kwargs["organization"] = self.get_organization()

readthedocs/subscriptions/notifications.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def for_subscriptions():
3131
return (
3232
djstripe.Subscription.readthedocs.trial_ending()
3333
.created_days_ago(24)
34-
.prefetch_related("customer__rtd_organization")
34+
.select_related("customer__rtd_organization")
3535
)
3636

3737

0 commit comments

Comments
 (0)