Skip to main content
=====
Prefetch Plus
=====

Prefetch Plus aims to add additional flexibility to django's built in
prefetch_related functionality.

Using the built in prefetch_related you can only select objects related through
reverse ForeignKey relationships. With PrefetchPlus you can slect objects related
through any mapping of columns from one table to another.

This allows forsomething much more similar to the capabilities of a
databases left outer join.

Installation
-----------
pip install django-prefetch-plus

Usage
-----------

As Manager:

Prefetch Plus comes with the PrefetchPlusQuerySet which provides a similar
interface to prefetch_related:
```
from prefetch_plus import PrefetchPlusQuerySet
from django.db import models

GENRES = (
...
('romance', 'Romance'),
('mystery', 'Mystery'),
)


class Author(models.Model)
...
genre = models.CharField(
choices = GENRES
)
...
objects = PrefetchPlusQuerySet.as_manager()

class Book(models.Model)
...
author = models.ForeignKey(
Author
)

genre = models.CharField(
choices = GENRES
)
...

# We want to have author's of books review other books in the same genre.

# The old way
for author in Author.objects.all():
# Typical N+1 db query that cannot be resolved with builtin prefetch_related
author.books_to_review = list(Book.objects.filter(genre=author.genre))

# The new way
# This will grab all of the Books that share a genre with any author,
# then it will attach attach the list of books in each genre to the matching
# author(s)
authors = Author.objects.prefetch_plus(
to_attr='books_to_review',
query_set=Book.objects.all(),
obj_cols='genre',
qset_cols='genre'
).all()
# obj_cols & qset_cols can also be tuples of columns to allow for
# "composite keys". The tuple must be the same length and the matches are
# getattr(object, obj_cols[i]) == getattr(qset_object, qset_cols[i]) for any i

# Prefetch plus works across related objects. I highly recommend you remember to
# select_related if you do this. Otherwise, you aren't getting any benefit out
# of this
authors = Author.objects.prefetch_plus(
to_attr='books_to_review',
query_set=Book.objects.select_related('author').all(),
obj_cols='genre',
qset_cols='author__genre'
).all()
```

Without the manager:

You don't have to use the manager if you don't want, it simply calls the
following helper function at the appropriate time

```
from prefetch_plus import do_prefetch_plus
from django.db import models

GENRES = (
...
('romance', 'Romance'),
('mystery', 'Mystery'),
)


class Author(models.Model)
...
genre = models.CharField(
choices = GENRES
)
...

class Book(models.Model)
...
author = models.ForeignKey(
Author
)

genre = models.CharField(
choices = GENRES
)
...

# We want to have author's of books review other books in the same genre.

authors = Author.objects.all()

do_prefetch_plus(
objects=authors,
to_attr='books_to_review',
query_set=Book.objects.all(),
obj_cols='genre',
qset_cols='genre'
)
```

Version History
-----------
0.3.3: Made Prefetch plus faster at the cost of potentially grabbing extra
records in the case of composite keys. Fixed support for 2.7

Release files for django-prefetch-plus 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-prefetch-plus 0.3.3
File Size Uploaded
django-prefetch-plus-0.3.3.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-prefetch-plus 0.3.3
File Interpreter ABI Platform
django_prefetch_plus-0.3.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 10.8 kB

Release files / django-prefetch-plus-0.3.3.tar.gz

Download URL django-prefetch-plus-0.3.3.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8d1367ad1a5db7b9b7fdd538509e65abd8c233bda8fd3ca04c569e2cf4ceac31
BLAKE2b-256 checksum
How to use checksums
c73b435d53f1b6be53b3c4dc07e7e8d9f36cedf68610093b7be66260f18e87a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_prefetch_plus-0.3.3-py2.py3-none-any.whl

Download URL django_prefetch_plus-0.3.3-py2.py3-none-any.whl
Size 6.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7467dd1c56ec1ddc083ace0b7a77bc8f69739a4ca875338a4bd9509e42bf48b5
BLAKE2b-256 checksum
How to use checksums
7da22540780f1b138c4d8be99c20353c8fa2a344cb7cc249633cd6a89985978f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page