Coder Social home page Coder Social logo

django-auto-prefetching's Introduction

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.7 and 3.8 and Django 3.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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.