| 
3 | 3 | from django.contrib.contenttypes.models import ContentType  | 
4 | 4 | from django.db.models import Exists  | 
5 | 5 | from django.db.models import OuterRef  | 
 | 6 | +from django.db.models import Prefetch  | 
6 | 7 | from rest_flex_fields import is_expanded  | 
7 | 8 | from rest_flex_fields.views import FlexFieldsMixin  | 
8 | 9 | from rest_framework import status  | 
 | 
39 | 40 | from readthedocs.oauth.models import RemoteRepositoryRelation  | 
40 | 41 | from readthedocs.organizations.models import Organization  | 
41 | 42 | from readthedocs.organizations.models import Team  | 
 | 43 | +from readthedocs.projects.models import Domain  | 
42 | 44 | from readthedocs.projects.models import EnvironmentVariable  | 
43 | 45 | from readthedocs.projects.models import Project  | 
44 | 46 | from readthedocs.projects.models import ProjectRelationship  | 
@@ -177,11 +179,23 @@ def get_queryset(self):  | 
177 | 179 |         # This could be a class attribute and managed on the ``ProjectQuerySetMixin`` in  | 
178 | 180 |         # case we want to extend the ``prefetch_related`` to other views as  | 
179 | 181 |         # well.  | 
180 |  | -        return queryset.prefetch_related(  | 
181 |  | -            "related_projects",  | 
182 |  | -            "domains",  | 
 | 182 | +        return queryset.select_related(  | 
 | 183 | +            "main_language_project",  | 
 | 184 | +        ).prefetch_related(  | 
183 | 185 |             "tags",  | 
184 | 186 |             "users",  | 
 | 187 | +            # Prefetch superprojects to avoid N+1 queries when serializing the project.  | 
 | 188 | +            Prefetch(  | 
 | 189 | +                "superprojects",  | 
 | 190 | +                ProjectRelationship.objects.all().select_related("parent"),  | 
 | 191 | +                to_attr="_superprojects",  | 
 | 192 | +            ),  | 
 | 193 | +            # Prefetch the canonical domain to avoid N+1 queries when using the resolver.  | 
 | 194 | +            Prefetch(  | 
 | 195 | +                "domains",  | 
 | 196 | +                Domain.objects.filter(canonical=True),  | 
 | 197 | +                to_attr="_canonical_domains",  | 
 | 198 | +            ),  | 
185 | 199 |         )  | 
186 | 200 | 
 
  | 
187 | 201 |     def create(self, request, *args, **kwargs):  | 
 | 
0 commit comments