Skip to main content

Improve performance and maintainability with a prefetching layer in your Django / Django REST Framework project

Project description

Django Virtual Models Icon

Django Virtual Models

Improve performance and maintainability with a prefetching layer in your Django / Django REST Framework project

Test   Coverage Status   Package version   Supported Python versions


Documentation: https://vintasoftware.github.io/django-virtual-models/

Source Code: https://github.com/vintasoftware/django-virtual-models


Django Virtual Models introduces a new "prefetching layer" to Django codebases that assists developers to express complex read logic without sacrificing maintainability, composability and performance. A Virtual Model allows developers to declare all nesting they need along with all annotations, prefetches, and joins in a single declarative class.

When implementing Django REST Framework serializers, developers need to be careful to avoid causing the N+1 selects problem due to missing prefetch_related or select_related calls on the associated queryset. Additionaly, developers must not miss annotate calls for fields that are computed at queryset-level.

With Virtual Models integration with DRF, if you change a DRF Serializer, you won't forget to modify the associated queryset with additional annotations, prefetches, and joins. If you do forget to update the queryset, Django Virtual Models will guide you by raising friendly exceptions to assist you to write the correct Virtual Model for the serializer you're changing. This guidance will prevent N+1s and missing annotations in all serializers that use Virtual Models.

For example, imagine if you have following nested serializers starting from MovieSerializer:

from movies.models import Nomination, Person, Movie

class AwardSerializer(serializers.ModelSerializer):
    class Meta:
        model = Nomination
        fields = ["award", "category", "year", "is_winner"]

class PersonSerializer(serializers.ModelSerializer):
    awards = AwardSerializer(many=True)
    nomination_count = serializers.IntegerField(read_only=True)

    class Meta:
        model = Person
        fields = ["name", "awards", "nomination_count"]

class MovieSerializer(serializers.ModelSerializer):
    directors = PersonSerializer(many=True)

    class Meta:
        model = Movie
        fields = ["name", "directors"]

Note that for proper performance and functionality, all nested serializers must have a corresponding prefetch_related on the queryset used by MovieSerializer. Also, the nomination_count field should be annotated on it. Therefore, you'll need to write this complex chain of nested prefetches:

from django.db.models import Prefetch

awards_qs = Nomination.objects.filter(is_winner=True)

directors_qs = Person.objects.prefetch_related(
    Prefetch(
        "nominations",
        queryset=awards_qs,
        to_attr="awards"
    )
).annotate(
    nomination_count=Count("nominations")
).distinct()

qs = Movie.objects.prefetch_related(
    Prefetch(
        "directors",
        queryset=directors_qs
    )
)

Conversely, you can declare Virtual Models for this read logic to easily reuse and customize those classes in multiple places of the codebase:

import django_virtual_models as v

class VirtualAward(v.VirtualModel):
    class Meta:
        model = Nomination

    def get_prefetch_queryset(self, **kwargs):
        return Nomination.objects.filter(is_winner=True)


class VirtualPerson(v.VirtualModel):
    awards = VirtualAward(
        manager=Nomination.objects,
        lookup="nominations",
        to_attr="awards",
    )
    nomination_count = v.Annotation(
        lambda qs, **kwargs: qs.annotate(
            nomination_count=Count("nominations")
        ).distinct()
    )

    class Meta:
        model = Person


class VirtualMovie(v.VirtualModel):
    directors = VirtualPerson(manager=Person.objects)

    class Meta:
        model = Movie

qs = VirtualMovie().get_optimized_queryset(
    Movie.objects.all(),
    lookup_list=[
        "directors__awards",
        "directors__nomination_count",
    ]
)

If, for example, you forget to add the nomination_count field on VirtualPerson, the following exception will appear when using MovieSerializer:

MissingVirtualModelFieldException exception

To configure your view and serializer to use Virtual Models, inherit from the proper classes:

import django_virtual_models as v

class MovieSerializer(v.VirtualModelSerializer):
    ...

class MovieList(v.VirtualModelListAPIView):
    queryset = Movie.objects.all()
    serializer_class = MovieSerializer

To learn more, check the Installation and the Tutorial. Or the example project.

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-virtual-models-0.1.0.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

django_virtual_models-0.1.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file django-virtual-models-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-virtual-models-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6f18db28b263bcec4c921466aac983ff9eaeebff57aeff85d00dd9e2062016d
MD5 c9dc6ec45d7f3ebceab565065f822c6d
BLAKE2b-256 7dd2c672b316887d6484c9d206c179388ddbd4fadd3f24fc6000638f60a39f20

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_virtual_models-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 663afc473ec558f2c71440860971f5c4edcb60ad9bd495bdb24a425e21d23fee
MD5 9ca39928f7c780147c5b98937857943e
BLAKE2b-256 e5375c5b02f8eeb703d02102350fcbc462a1e3a6cfb0ccc6f78cc612cbdf6fdd

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