Tools for automatically prefetching related objects in Django and Django-rest-framework
Never worry about n+1 performance problems again
This project aims to automatically perform the correct
calls 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.
pip install django-auto-prefetching
This is a ViewSet mixin you can use, which will automatically prefetch the needed objects from the database. In most circumstances this will be all the database optimizations you'll ever need to do:
Simply add it after your ModelViewSet class.
from django_auto_prefetching import AutoPrefetchViewSetMixin from rest_framework.viewsets import ModelViewSet class BaseModelViewSet(AutoPrefetchViewSetMixin, ModelViewSet): queryset = YourModel.objects.all() serializer_class = YourModelSerializer
It supports all types of relational fields, (many to many, one to many, one to one, etc.) out of the box.
AutoPrefetchViewSetMixin cannot see what objects are being accessed in e.g. a
If you use objects in there, you might need to do some additional prefetches.
from django_auto_prefetching import AutoPrefetchViewSetMixin from rest_framework.viewsets import ModelViewSet class BaseModelViewSet(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 queryset
Currently the project is only being tested against the latest version of Python (3.7) and the latest version of Django(2.2) Pull Requests to support earlier versions of Django are welcome.
The project is currently being used without issues in a medium-sized Django project(20.000 lines of code)
Contributions are welcome! To get the tests running, do the following:
- Clone the repository.
- If you don't have it, install pipenv
- Install the dependencies with
pipenv sync --dev
- Activate the virtualenv created by pipenv by writing
- Run the tests with
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size django_auto_prefetching-0.1.8-py2.py3-none-any.whl (22.2 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size django_auto_prefetching-0.1.8.tar.gz (9.4 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for django_auto_prefetching-0.1.8-py2.py3-none-any.whl
Hashes for django_auto_prefetching-0.1.8.tar.gz