Package: django-auto-prefetching
Django Auto-PrefetchingNever worry about n+1 performance problems againThis project aims to automatically perform the correctselect_relatedandprefetch_relatedcalls for your django-rest-framework code. It does this by inspecting your serializers, seeing what fields
they use, and what models they refer to, and automatically calculating what needs to be prefetched.InstallationInstallation viapip:pip install django-auto-prefetchingAutoPrefetchViewSetMixinThis is a ViewSet mixin you can use, which will automatically prefetch the needed objects from the database, based on the ViewSetsquerysetandserializer_class. Under most circumstances this will be all the database optimizations you'll ever need to do:UsageSimply add it after your ModelViewSet class.fromdjango_auto_prefetchingimportAutoPrefetchViewSetMixinfromrest_framework.viewsetsimportModelViewSetclassBaseModelViewSet(AutoPrefetchViewSetMixin,ModelViewSet):queryset=YourModel.objects.all()serializer_class=YourModelSerializerIt supports all types of relational fields (many to many, one to many, one to one etc.) out of the box.Manually calling prefetchTheAutoPrefetchViewSetMixincannot see what objects are being accessed in e.g. aSerializerMethodField.
If you use objects in there, you might need to do some additional prefetches.
If you do this and overrideget_queryset, you will have to callprefetchmanually as the mixin code is never reached.importdjango_auto_prefetchingfromrest_framework.viewsetsimportModelViewSetclassBaseModelViewSet(django_auto_prefetching.AutoPrefetchViewSetMixin,ModelViewSet):serializer_class=YourModelSerializerdefget_queryset(self):# Simply do the extra select_related / prefetch_related here# and leave the mixin to do the rest of the workqueryset=YourModel.objects.all()queryset=queryset.select_related('my_extra_field')returndjango_auto_prefetching.prefetch(queryset,self.serializer_class)You can overrideget_prefetchable_querysetinstead ofget_querysetif you don't want to manually calldjango_auto_prefetching.prefetch(). Example:importdjango_auto_prefetchingfromrest_framework.viewsetsimportModelViewSetclassBaseModelViewSet(django_auto_prefetching.AutoPrefetchViewSetMixin,ModelViewSet):serializer_class=YourModelSerializerdefget_prefetchable_queryset(self):returnYourModel.objects.all()Nowget_queryset()will call ourget_prefetchable_queryset()and will add automatic prefetchesManually specifying which fields are neededIf you need to explicitly specify some extra fields to be included or excluded, you can also override the following methods on your ViewSet to return a list or a set of fields to prefetch/exclude.importdjango_auto_prefetchingfromrest_framework.viewsetsimportModelViewSetclassBaseModelViewSet(django_auto_prefetching.AutoPrefetchViewSetMixin,ModelViewSet):serializer_class=YourModelSerializerdefget_auto_prefetch_excluded_fields(self):return{"exclude_this_field","and_this_field"}defget_auto_prefetch_extra_select_fields(self):return{"select_related_on_this_field"}defget_auto_prefetch_extra_prefetch_fields(self):return{"prefetch_related_on_this_field"}}Supported VersionsPython: 3.7, 3.8, 3.10Django: 3.2, 4.0.4Pull Requests to support other versions are welcome.MaturityThe project is currently being used without issues in a medium-sized Django project (20.000 lines of code).ContributingContributions are welcome! To get the tests running, do the following:Clone the repository.If you don't have it, installpipenvInstall the dependencies withpipenv sync --devActivate the virtualenv created by pipenv by writingpipenv shellRun the tests with./manage.py test