Skip to content

Instantly share code, notes, and snippets.

@mathjazz
Last active June 29, 2025 22:20
Show Gist options
  • Select an option

  • Save mathjazz/2258fbaf654011abc1b4ac4b9a96bcf1 to your computer and use it in GitHub Desktop.

Select an option

Save mathjazz/2258fbaf654011abc1b4ac4b9a96bcf1 to your computer and use it in GitHub Desktop.
Move prefetch_related() on the ProjectListView to also have impact when include_disabled and include_system are used
diff --git a/pontoon/api/serializers.py b/pontoon/api/serializers.py
index ffd6f0b4a..1c8b41ce3 100644
--- a/pontoon/api/serializers.py
+++ b/pontoon/api/serializers.py
@@ -107,7 +107,7 @@ class NestedProjectSerializer(ProjectSerializer):
fields = ProjectSerializer.Meta.fields + ["tags", "locales"]
def get_locales(self, obj):
- return list(obj.project_locale.values_list("locale__code", flat=True))
+ return [projectLocale.locale.code for projectLocale in obj.project_locale.all()]
class NestedLocaleSerializer(LocaleSerializer):
@@ -117,7 +117,9 @@ class NestedLocaleSerializer(LocaleSerializer):
fields = LocaleSerializer.Meta.fields + ["projects"]
def get_projects(self, obj):
- return [pl.project.slug for pl in obj.project_locale.all()]
+ return [
+ projectLocale.project.slug for projectLocale in obj.project_locale.all()
+ ]
class NestedProjectLocaleSerializer(ProjectLocaleSerializer):
diff --git a/pontoon/api/views.py b/pontoon/api/views.py
index 80b72be4b..87da44ebf 100644
--- a/pontoon/api/views.py
+++ b/pontoon/api/views.py
@@ -141,7 +141,13 @@ class ProjectListView(generics.ListAPIView):
include_disabled = query_params.get("include_disabled")
include_system = query_params.get("include_system")
- base_queryset = Project.objects.visible().visible_for(self.request.user)
+ base_queryset = (
+ Project.objects.visible()
+ .visible_for(self.request.user)
+ .prefetch_related("project_locale__locale", "tags")
+ .select_related("contact")
+ )
+
filters = Q()
if include_disabled is not None:
filters |= Q(disabled=True)
@@ -155,8 +161,10 @@ class ProjectListView(generics.ListAPIView):
class ProjectIndividualView(generics.RetrieveAPIView):
- queryset = Project.objects.all().prefetch_related(
- "project_locale__locale", "contact"
+ queryset = (
+ Project.objects.all()
+ .prefetch_related("project_locale__locale")
+ .select_related("contact")
)
serializer_class = NestedProjectSerializer
lookup_field = "slug"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment