Skip to main content

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

Project description

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

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.7.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

django_auto_prefetching-0.1.7-py2.py3-none-any.whl (22.2 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django_auto_prefetching-0.1.7.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for django_auto_prefetching-0.1.7.tar.gz
Algorithm Hash digest
SHA256 c0abeaa70555f1cc18a44cf5da0a39b4dc0bad81926ec4c3b5bcce9d3ceb762e
MD5 3600c6bfbc6c2cbb7eed597d8da96faf
BLAKE2b-256 6cb6d7f65e9ce8f00baea4e366fd27742310e2d118e47bb2516ce2aedb8dfdad

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: django_auto_prefetching-0.1.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for django_auto_prefetching-0.1.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb31c772c309b5334d4db2e6268aa83176d6973469bf0e1f25a29473e07e6a87
MD5 26bcd20f7db36111ef2ed0ce033c41e5
BLAKE2b-256 81c8e4a7fd25b6d838a56052d502318b33b9adef9ca24f64bb43b6750afe2571

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