django - DRF ModelViewSet ignore prefetch/select_related? -
i know question has been asked few times solutions doesn't work me.
i'm using django-debug-toolbar , see there thousands of queries made related objects. there 2k rows in model, , takes 10 seconds drf's api page load.
i've tried using possible combinations of prefetch/select_related don't work. here's 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 =
the biggest culprits pagination class , serializer. however, couldn't find suspicious in drf's built-in pagination class, while waiting page load, put print statement on places in pagination class , printed immediately, that's not bottle neck.
next suspicion modelserializer, not sure what's happening. originally, debug-toolbar shows there around 1k queries every page 8 items per page, , did this:
class myviewset(viewsets.modelviewset): serializer_class = aserializer queryset = [] pagination_class = pagination.pagenumberpagination
to surprise, although it's showing 0 items, debug-toolbar still shows there ~1k queries , still takes long time load. if change serializer class other model 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, , there less queries. assumption somehow modelserializer loads "almost" entire result set in iteration processes, couldn't find it. reason said "almost" because there difference between number of total rows (about 2k) , number of queries reported in debug toolbar (1k). ideas?
Comments
Post a Comment