Fixed #17001 -- Custom querysets for prefetch_related.

This patch introduces the Prefetch object which allows customizing prefetch
operations.

This enables things like filtering prefetched relations, calling select_related
from a prefetched relation, or prefetching the same relation multiple times
with different querysets.

When a Prefetch instance specifies a to_attr argument, the result is stored
in a list rather than a QuerySet. This has the fortunate consequence of being
significantly faster. The preformance improvement is due to the fact that we
save the costly creation of a QuerySet instance.

Thanks @akaariai for the original patch and @bmispelon and @timgraham
for the reviews.
This commit is contained in:
Loic Bistuer 2013-11-07 00:25:05 +07:00 committed by Anssi Kääriäinen
parent b1b04df065
commit f51c1f5900
9 changed files with 616 additions and 65 deletions

View file

@ -137,6 +137,9 @@ class TaggedItem(models.Model):
def __str__(self):
return self.tag
class Meta:
ordering = ['id']
class Bookmark(models.Model):
url = models.URLField()
@ -146,6 +149,9 @@ class Bookmark(models.Model):
object_id_field='favorite_fkey',
related_name='favorite_bookmarks')
class Meta:
ordering = ['id']
class Comment(models.Model):
comment = models.TextField()
@ -155,12 +161,16 @@ class Comment(models.Model):
object_pk = models.TextField()
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
class Meta:
ordering = ['id']
## Models for lookup ordering tests
class House(models.Model):
address = models.CharField(max_length=255)
owner = models.ForeignKey('Person', null=True)
class Meta:
ordering = ['id']