prefetch_related() failure in django 1.6
Trying to migrate our Django 1.5 application to 1.6. I hit what I think is a bug, or at the very least very confusing behavior.
-- Imagine some models like the following:
class Img(Model):
uuid = CharField()
class ImgResolutions(Model):
img = ForeignKey(Img, related_name='resolutions')
url = CharField()
# Lots of things have images, this is just one, so I want to leave off the related name, just as is documented in
# https://docs.djangoproject.com/en/1.6/ref/models/fields/#django.db.models.ForeignKey.related_name
class Place(Model):
detail_img = ForeignKey(Img, related_name='+')
icon_img = ForeignKey(Img, related_name='+')
Now, when I do a query like this:
places = Place.objects.filter(.. whatever ..).prefetch_related('detail_img__resolutions')
I get something like:
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
self._fetch_all()
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 856, in _fetch_all
self._prefetch_related_objects()
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 517, in _prefetch_related_objects
prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 1598, in prefetch_related_objects
obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 1697, in prefetch_one_level
prefetcher.get_prefetch_queryset(instances)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 277, in get_prefetch_queryset
qs = self.get_queryset(instance=instances[0]).filter(**query)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 590, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 608, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1202, in add_q
clause = self._add_q(where_part, used_aliases)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1236, in _add_q
current_negated=current_negated)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1101, in build_filter
allow_explicit_fk=True)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1356, in setup_joins
names, opts, allow_many, allow_explicit_fk)
File "/postmates/virtualenv.d/postal-main/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1279, in names_to_path
"Choices are: %s" % (name, ", ".join(available)))
FieldError: Cannot resolve keyword '+' into field. Choices are: date_created, date_updated, deleted, id, resolutions, uuid
From what I can tell, this has something to do with the addition of related_query_name.
For some reason, it's trying to build a filter that looks like '+_in', because of L276 in django/db/models/fields/related.py :
query = {'%s__in' % self.field.related_query_name(): instances}
I think it's trying to build a query like "Img.place in [place list]"
Am I doing something wrong or is this a bug?
Thanks,
Rhett
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9deabf4a-61a2-48c6-9ad7-756b65d360e5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home