Skip to main content

More generic form of django prefetch_related

Project description

=====
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

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-plus-0.3.3.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

django_prefetch_plus-0.3.3-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-prefetch-plus-0.3.3.tar.gz.

File metadata

File hashes

Hashes for django-prefetch-plus-0.3.3.tar.gz
Algorithm Hash digest
SHA256 8d1367ad1a5db7b9b7fdd538509e65abd8c233bda8fd3ca04c569e2cf4ceac31
MD5 8fac9a5059e5c905d2c66c6c36410a98
BLAKE2b-256 c73b435d53f1b6be53b3c4dc07e7e8d9f36cedf68610093b7be66260f18e87a0

See more details on using hashes here.

File details

Details for the file django_prefetch_plus-0.3.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_prefetch_plus-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7467dd1c56ec1ddc083ace0b7a77bc8f69739a4ca875338a4bd9509e42bf48b5
MD5 bc4f262aee9a0adddf732f3480e6e05f
BLAKE2b-256 7da22540780f1b138c4d8be99c20353c8fa2a344cb7cc249633cd6a89985978f

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