perf(organizations): speed up filtered API lists
This commit is contained in:
@@ -285,9 +285,7 @@ class CachedReadOnlyMixin:
|
||||
class OrganizationViewSet(CachedReadOnlyMixin, ReadOnlyModelViewSet):
|
||||
"""Read-only API for canonical organizations."""
|
||||
|
||||
queryset = Organization.objects.select_related("data_snapshot").order_by(
|
||||
"name", "uid"
|
||||
)
|
||||
queryset = Organization.objects.order_by("name", "uid")
|
||||
serializer_class = OrganizationSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
lookup_field = "uid"
|
||||
@@ -307,7 +305,10 @@ class OrganizationViewSet(CachedReadOnlyMixin, ReadOnlyModelViewSet):
|
||||
return super().get_permissions()
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = super().get_queryset().select_related("data_snapshot")
|
||||
if self._should_defer_snapshot_data():
|
||||
queryset = queryset.defer("data_snapshot__data")
|
||||
|
||||
if self.action != "list" or "has_registry" in self.request.query_params:
|
||||
return queryset
|
||||
|
||||
@@ -320,6 +321,20 @@ class OrganizationViewSet(CachedReadOnlyMixin, ReadOnlyModelViewSet):
|
||||
return filterset.qs
|
||||
return queryset
|
||||
|
||||
def _should_defer_snapshot_data(self) -> bool:
|
||||
if getattr(self, "action", None) != "list":
|
||||
return False
|
||||
|
||||
return not any(
|
||||
name in self.request.query_params
|
||||
for name in (
|
||||
"data",
|
||||
"data_sources",
|
||||
"exclude_data",
|
||||
"exclude_data_sources",
|
||||
)
|
||||
)
|
||||
|
||||
@swagger_auto_schema(
|
||||
tags=[ORGANIZATIONS_TAG],
|
||||
operation_id="v2_organizations_list",
|
||||
|
||||
Reference in New Issue
Block a user