Skip to main content

Auto generate Django ORM query sets from serializers.

Project description

drf-auto-query

Auto generate Django ORM query sets from serializers.

Installation

$ pip install drf-auto-query

Description

The "drf-auto-query" package addresses the common problem of N+1 queries in Django ORM when building REST framework endpoints. It provides convenient helper functions and mixins to assist in generating efficient QuerySet objects tailored to your specific serializer requirements.

It is important to note that while "drf-auto-query" aims to alleviate the N+1 problem, it is not a comprehensive solution. Instead, it serves as a valuable tool to be used in conjunction with your own efforts to write efficient queries. By automating the process of prefetching related data, the package helps reduce the number of database queries required, resulting in improved performance and responsiveness for your Django-based APIs, while it also speeds up development because you will not have to worry about your serializer setup firing unwanted database queries.

Usage

Prefetch function

The prefetch_queryset_for_serializer function streamlines the process of setting up the necessary prefetch_related and select_related calls on a QuerySet based on a given serializer class.

from rest_framework import serializers
from drf_auto_query import prefetch_queryset_for_serializer

from my_app.models import UserGroup


class UserSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    username = serializers.CharField()
    email = serializers.EmailField()

    
class UserGroupSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    name = serializers.CharField()
    users = UserSerializer(many=True)
    
    
queryset = UserGroup.objects.all()
queryset = prefetch_queryset_for_serializer(queryset, UserGroupSerializer)

QuerySet mixin

The AutoQuerySetMixin offers a convenient way to automatically prefetch the required relations on a QuerySet.

from django.db import models

from drf_auto_query.mixins import AutoQuerySetMixin


class MyModelQuerySet(AutoQuerySetMixin, models.Model):
    ...
    
    
class MyModel(models.Model):
    ...
    
    objects = models.Manager.from_queryset(MyModelQuerySet)()

This can then be used in a class based view.

from rest_framework import generics

from my_app.models import MyModel
from my_app.serializers import MyModelSerializer


class MyModelList(generics.ListAPIView):
    serializer_class = MyModelSerializer
    queryset = MyModel.objects.prefetch_for(serializer_class)

Using the both the mixin and the prefetch function ensures that any annotations, joins, or other modifications to the QuerySet are preserved while automatically prefetching the necessary data for efficient serialization.

:warning: Any multi-nested prefetches that have altered querysets might be overwritten. This is a known issue and will be addressed in a future release.

Example of a multi-nested prefetch:

MyModel.objects.prefetch_related(
    Prefetch(
        'my_related_model',
        queryset=MyRelatedModel.objects.prefetch_related(
            Prefetch(
                'my_other_related_model',
                # This queryset might not be preserved.
                queryset=MyOtherRelatedModel.objects.annotate(count_something=Count('something'))
            )
        )
    )
)

MyModel.objects.prefetch_related(
    Prefetch(
        'my_related_model__my_other_related_model',
        # This will be preserved.
        queryset=MyOtherRelatedModel.objects.annotate(count_something=Count('something'))
    )
)

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

drf-auto-query was created by Jakob Verlic. It is licensed under the terms of the MIT license.

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

drf_auto_query-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_auto_query-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file drf_auto_query-0.1.0.tar.gz.

File metadata

  • Download URL: drf_auto_query-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.7.15 Darwin/22.1.0

File hashes

Hashes for drf_auto_query-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a7385178f67e3d2a83e432d2d843b2a681e1291863e44a00f08cee7ae48f619
MD5 207193104601b96b7de0fe15d3c34232
BLAKE2b-256 09fb7422aadae3da461ddb86479d7003737d788b81c45cee56586cfd7923d78f

See more details on using hashes here.

File details

Details for the file drf_auto_query-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: drf_auto_query-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.7.15 Darwin/22.1.0

File hashes

Hashes for drf_auto_query-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7864117b88b63f3bfcb6d2c12e48e894eb6d65e50410a1a1e44099494e15d234
MD5 98fc4bedf7b8bcfd44031965ef52831b
BLAKE2b-256 53e25c00a72141ee9cf207741d2d4b932550b190f5d1dfff9e7a13e88d6a216a

See more details on using hashes here.

Supported by

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