Skip to main content

Tools for automatically prefetching related objects in Django and Django-rest-framework

Project description

Django Auto-Prefetching

<GeeWee>

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.

Manually calling prefetch

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. If you do this and override get_queryset, you will have to call prefetch manually as the mixin code is never reached.

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)

Supported Versions

Currently the project is currently being tested against Python 3.6 and 3.7 and Django 2.2 Pull Requests to support other versions 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_auto_prefetching-0.1.9.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

django_auto_prefetching-0.1.9-py2.py3-none-any.whl (23.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_auto_prefetching-0.1.9.tar.gz.

File metadata

  • Download URL: django_auto_prefetching-0.1.9.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.9

File hashes

Hashes for django_auto_prefetching-0.1.9.tar.gz
Algorithm Hash digest
SHA256 0b07be02a9f0d01251137a4d42b3025d400937b6c46f0d84999e00afdfe5e1c3
MD5 4082a2b41375dc8b982a872968fe9dcf
BLAKE2b-256 1f006aa5cdf601d830b74e0c9195d92e88cdcc563efabc117054812426b7b914

See more details on using hashes here.

Provenance

File details

Details for the file django_auto_prefetching-0.1.9-py2.py3-none-any.whl.

File metadata

  • Download URL: django_auto_prefetching-0.1.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.9

File hashes

Hashes for django_auto_prefetching-0.1.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4fe760bef1ad2948d1c988710525bbd1f3adc52328da8703ba1de45cb1ba535d
MD5 fe4353e845d8e804c80d0685d51a2c37
BLAKE2b-256 91babf4a9c24ddbaf8428a67c2aba693ac06e680b67121c8af306124eba3a2f6

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page