prefetch_related() failure in django 1.6

80 views
Skip to first unread message

Rhett G.

unread,
Nov 21, 2013, 7:52:29 PM11/21/13
to django...@googlegroups.com
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
  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

Anssi Kääriäinen

unread,
Nov 22, 2013, 5:45:04 AM11/22/13
to django...@googlegroups.com
This will be fixed in next minor version of Django 1.6.

 -Anssi

tim

unread,
Nov 22, 2013, 6:58:17 AM11/22/13
to django...@googlegroups.com
It may be https://code.djangoproject.com/ticket/21410? If so, that'll be fixed in 1.6.1.
Reply all
Reply to author
Forward
0 new messages