I am totally stumped. I have two models (Company and Customer). I am trying to get a list of all companies that have served this particular customer, but for legacy DB reasons, I can't use foreign keys and am instead relying on a bit of a funky materialized view setup. But for reasons that are absolutely baffling to me, DRF is taking forever to manage the request when there are a large number of customers (User CPU time 145203.809 msec) even though the SQL query is very fast... (329.21 ms).
Here's the code. Any idea why DRF would be choking?
class CompanyListView(generics.ListAPIView):
def get_queryset(self):
queryset = Company.objects.all()
if 'customer' in self.request.query_params:
customer = self.request.query_params["customer"]
companies = MaterializedViewLookupTable.objects.filter(customer=customer).values_list(
"company_id", flat=True
)
queryset = queryset.filter(
company_id__in=[*companies],
)
return queryset
lookup_url_kwarg = "company_id"
serializer_class = CompanySeralizer
filterset_class = CompanyFilter
pagination_class = StandardResultsSetPagination
permission_classes = [permissions.IsAuthenticated]