Skip to content
Automatic prefetching for Django
Python
Branch: master
Clone or download
Latest commit 37f3ea8 Aug 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Ci (#4) Aug 17, 2019
django_auto_prefetching Fixes Hyperlinkedidentityfield (#5) Aug 17, 2019
test_project
.env added setup.py May 4, 2019
.gitignore
LICENSE
Pipfile added setup.py May 4, 2019
Pipfile.lock updated deps (#7) Aug 17, 2019
README.md updated the readme May 28, 2019
TODO.txt added todo May 21, 2019
db.sqlite3 first commit Apr 15, 2019
manage.py
setup.py updated setup.py (#8) Aug 17, 2019

README.md

Django Auto-Prefetching

Never worry about n+1 performance problems again

This project aims to automatically perform the correct select_related and prefetch_related 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.

Installation

pip install django_auto_prefetching

AutoPrefetchViewSetMixin

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:

Usage

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.

Limitations

The AutoPrefetchViewSetMixin cannot see what objects are being accessed in e.g. a SerializerMethodField. 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

Supported Versions

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.

Maturity

The project is currently being used without issues in a medium-sized Django project(20.000 lines of code)

Contributing

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 pipenv shell
  • Run the tests with ./manage.py test

LICENSE:

MIT

You can’t perform that action at this time.