Prefetch_related fails with GenericRelation
|Reported by:||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
While greatly anticipating prefetch_related, I am sorry to say that bug #17838 is not yet entirely fixed. I am running into empty result sets when I perform a prefetch_related.
I have a GenericRelation on my model to Comments. My model uses an integer primary key. This combination makes the prefetch_related return empty embedded result sets.
In the latest django, django/db/models/query.py, line 1791, the _prefetched_objects_cache is accessed with (possibly) an integer as value. However, the _prefetched_objects_cache is always populated with unicode values (lines 1770 - 1774)
The quick fix is to replace (line 1791)
obj._prefetched_objects_cache[cache_name] = qs
obj._prefetched_objects_cache[unicode(cache_name)] = qs
Can someone create a patch for this, and write a test to see if the queryset is actually populated with related items?