Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored October 29, 2012
6  django/contrib/contenttypes/generic.py
@@ -5,7 +5,6 @@
5 5
 
6 6
 from collections import defaultdict
7 7
 from functools import partial
8  
-from operator import attrgetter
9 8
 
10 9
 from django.core.exceptions import ObjectDoesNotExist
11 10
 from django.db import connection
@@ -329,8 +328,11 @@ def get_prefetch_query_set(self, instances):
329 328
                     set(obj._get_pk_val() for obj in instances)
330 329
                 }
331 330
             qs = super(GenericRelatedObjectManager, self).get_query_set().using(db).filter(**query)
  331
+            # We (possibly) need to convert object IDs to the type of the
  332
+            # instances' PK in order to match up instances:
  333
+            object_id_converter = instances[0]._meta.pk.to_python
332 334
             return (qs,
333  
-                    attrgetter(self.object_id_field_name),
  335
+                    lambda relobj: object_id_converter(getattr(relobj, self.object_id_field_name)),
334 336
                     lambda obj: obj._get_pk_val(),
335 337
                     False,
336 338
                     self.prefetch_cache_name)
10  tests/modeltests/prefetch_related/models.py
@@ -125,6 +125,10 @@ class TaggedItem(models.Model):
@@ -132,7 +136,11 @@ def __str__(self):
10  tests/modeltests/prefetch_related/tests.py
@@ -319,6 +319,16 @@ def test_generic_relation(self):

0 notes on commit 4c4d085

Please sign in to comment.
Something went wrong with that request. Please try again.