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

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


pip install django-prefetch-utils


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.

Files for django-prefetch-utils, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size django-prefetch-utils-0.1.0.tar.gz (51.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page