Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17991 - prefetch_related fails with GenericRelation and varcha…

…r ID field

Thanks to okke@formsma.nl for the report, and carmandrew@gmail.com for the tests.
  • Loading branch information...
commit 4c4d08502cbef6e0ed85470b2a5aade7fafa099f 1 parent 4ea8105
Luke Plant spookylukey authored
6 django/contrib/contenttypes/generic.py
View
@@ -5,7 +5,6 @@
from collections import defaultdict
from functools import partial
-from operator import attrgetter
from django.core.exceptions import ObjectDoesNotExist
from django.db import connection
@@ -329,8 +328,11 @@ def get_prefetch_query_set(self, instances):
set(obj._get_pk_val() for obj in instances)
}
qs = super(GenericRelatedObjectManager, self).get_query_set().using(db).filter(**query)
+ # We (possibly) need to convert object IDs to the type of the
+ # instances' PK in order to match up instances:
+ object_id_converter = instances[0]._meta.pk.to_python
return (qs,
- attrgetter(self.object_id_field_name),
+ lambda relobj: object_id_converter(getattr(relobj, self.object_id_field_name)),
lambda obj: obj._get_pk_val(),
False,
self.prefetch_cache_name)
10 tests/modeltests/prefetch_related/models.py
View
@@ -125,6 +125,10 @@ class TaggedItem(models.Model):
related_name='taggeditem_set3')
created_by_fkey = models.PositiveIntegerField(null=True)
created_by = generic.GenericForeignKey('created_by_ct', 'created_by_fkey',)
+ favorite_ct = models.ForeignKey(ContentType, null=True,
+ related_name='taggeditem_set4')
+ favorite_fkey = models.CharField(max_length=64, null=True)
+ favorite = generic.GenericForeignKey('favorite_ct', 'favorite_fkey')
def __str__(self):
return self.tag
@@ -132,7 +136,11 @@ def __str__(self):
class Bookmark(models.Model):
url = models.URLField()
- tags = generic.GenericRelation(TaggedItem)
+ tags = generic.GenericRelation(TaggedItem, related_name='bookmarks')
+ favorite_tags = generic.GenericRelation(TaggedItem,
+ content_type_field='favorite_ct',
+ object_id_field='favorite_fkey',
+ related_name='favorite_bookmarks')
class Comment(models.Model):
10 tests/modeltests/prefetch_related/tests.py
View
@@ -319,6 +319,16 @@ def test_generic_relation(self):
for t in b.tags.all()]
self.assertEqual(sorted(tags), ["django", "python"])
+ def test_charfield_GFK(self):
+ b = Bookmark.objects.create(url='http://www.djangoproject.com/')
+ t1 = TaggedItem.objects.create(content_object=b, tag='django')
+ t2 = TaggedItem.objects.create(content_object=b, favorite=b, tag='python')
+
+ with self.assertNumQueries(3):
+ bookmark = Bookmark.objects.filter(pk=b.pk).prefetch_related('tags', 'favorite_tags')[0]
+ self.assertEqual(sorted([i.tag for i in bookmark.tags.all()]), ["django", "python"])
+ self.assertEqual([i.tag for i in bookmark.favorite_tags.all()], ["python"])
+
class MultiTableInheritanceTest(TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.