Skip to main content

An library of utilities and enhancements for Django's prefetch_related system.

Project description

This library currently provides a replacement implementation of prefetch_related_objects which uses an identity map to automatically reduce the number of queries performed when prefetching.

For example, considered the following data model:

class Toy(models.Model):
    dog = models.ForeignKey('dogs.Dog')

class Dog(models.Model):
    name = models.CharField()
    favorite_toy = models.ForeignKey('toys.Toy', null=True)

With this library, we get don’t need to do a database query to perform the prefetch for favorite_toy since that object had already been fetched as part of the prefetching for toy_set:

>>> dog = Dog.objects.prefetch_related('toys', 'favorite_toy')[0]
SELECT * from dogs_dog limit 1;
SELECT * FROM toys_toy where toys_toy.dog_id IN (1);
>>> dog.favorite_toy is dog.toy_set.all()[0]  # no queries done
True

The plan is to increase the scope of the library in future versions to provide additional tools for working with prefetch_related.

  • Free software: BSD 3-Clause License

Installation

pip install django-prefetch-utils

Documentation

https://django-prefetch-utils.readthedocs.io/

Changelog

0.1.0 (2019-07-16)

  • First release on PyPI.

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-prefetch-utils-0.1.0.tar.gz (51.6 kB view hashes)

Uploaded Source

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