Django-auto-prefetching

Django-auto-prefetching is a small django package to do the select_related and prefetch_related automatically.

Usage

The usage is very simple, just import the AutoPrefetchViewSetMixin from django_auto_prefetching, and then add the mixin as the base class of a ListAPIView

If you override the get_queryset of the ListAPIView, need some work to make it work.


import django_auto_prefetching
from rest_framework.viewsets import ModelViewSet

class BaseModelViewSet(django_auto_prefetching.AutoPrefetchViewSetMixin, ModelViewSet):
    serializer_class = YourModelSerializer

    def get_queryset(self):
            # Simply do the extra select_related / prefetch_related here
            # and leave the mixin to do the rest of the work
            queryset = YourModel.objects.all()
            queryset = queryset.select_related('my_extra_field')
            return django_auto_prefetching.prefetch(queryset, self.serializer_class)

The problem it solves

Django rest framework has serious N+1 problem when generating output with Serializer, especially when you have nested serializer and the depth is more than 1.

This package will do the magic automatically.

References