I'm using django-debug-toolbar and see there are thousands of queries made to related objects. Basically there are about 2k rows in the model, and it takes about 10 seconds for DRF's API page to load.
I've tried using all possible combinations of prefetch/select_related but they don't work. Here's my code:
class A(models.Model):
b = models.ForeignKey(B)
class B(models.Model):
pass
class MyViewSet(viewsets.ModelViewSet):
serializer_class = ASerializer
queryset = A.objects.all().select_related('b')
pagination_class = pagination.PageNumberPagination
def get_queryset(self, *args, **kwargs):
return super(MyViewSet, self).get_queryset(*args, **kwargs).select_related('b')
class ASerializer(serializers.ModelSerializer):
class Meta:
model = A
The biggest culprits are the pagination class and the serializer. However, I couldn't find anything suspicious in DRF's built-in pagination class, while waiting for the page to load, I put some print statement on all places in the pagination class and they are all printed immediately, so definitely that's not the bottle neck.
Next suspicion is the ModelSerializer, I am not exactly sure what's happening. Originally, debug-toolbar shows there are around 1k queries for every page with 8 items per page, and then I did this:
class MyViewSet(viewsets.ModelViewSet):
serializer_class = ASerializer
queryset = []
pagination_class = pagination.PageNumberPagination
To my surprise, although it's now showing 0 items, but debug-toolbar still shows there are ~1k queries and it still takes a long time to load. Now if I change the serializer class into some other model which has far lesser rows:
class MyViewSet(viewsets.ModelViewSet):
serializer_class = BSerializer
queryset = []
pagination_class = pagination.PageNumberPagination
class BSerializer(serializers.ModelSerializer):
class Meta:
model = B
The page loads faster, and there are less queries. My only assumption now is that somehow ModelSerializer loads the "almost" the entire result set in some iteration processes, but I couldn't find it. The reason I said "almost" is because there are difference between the number of total rows (about 2k) and the number of queries reported in debug toolbar (1k). Any ideas?