Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

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


Release history Release notifications

This version
History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3

History Node

0.2

History Node

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
django_prefetch_plus-0.3.3-py2.py3-none-any.whl (6.0 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Aug 30, 2016
django-prefetch-plus-0.3.3.tar.gz (4.7 kB) Copy SHA256 hash SHA256 Source None Aug 30, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page