为什么prefetch_related()会引发AttributeError?

时间:2015-09-09 17:07:43

标签: django django-queryset django-hvad

我有一个与第二个模型有多对多关系的模型。当我尝试使用prefetch_related时,我得到一个AttributeError。模型可以概括为:

class AcademicSubject(TranslatableModel):
    translations = TranslatedFields(
        name = CharField(max_length=255)
    )

class UserProfile(Model):
    user = OneToOneField(settings.AUTH_USER_MODEL)
    # ... other fields
    subjects = ManyToManyField(AcademicSubject, blank=True)

(省略各种细节)。 TranslatableModelTranslatedFields来自django-hvad,虽然我不相信它们在这种情况下非常重要,但我并不是100%确定,所以我认为我&#39 ; d包括那个。

我尝试迭代的查询集是:

UserProfile.objects.all().select_related('user').prefetch_related('subjects')

我得到的堆栈跟踪是:

Traceback:
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  206.             return view(request, *args, **kwargs)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/home/hamish/dev/django/website/mentor_reports/views.py" in get
  45.         for item in self.get_queryset():
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  141.         self._fetch_all()
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  968.             self._prefetch_related_objects()
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/query.py" in _prefetch_related_objects
  611.         prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/query.py" in prefetch_related_objects
  1791.                 obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/query.py" in prefetch_one_level
  1906.         rel_attr_val = rel_obj_attr(rel_obj)
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in <lambda>
  912.                     lambda result: tuple(getattr(result, '_prefetch_related_val_%s' % f.attname) for f in fk.local_related_fields),
File "/home/hamish/dev/django/website/.ve/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in <genexpr>
  912.                     lambda result: tuple(getattr(result, '_prefetch_related_val_%s' % f.attname) for f in fk.local_related_fields),

Exception Type: AttributeError at /en/admin/user_report
Exception Value: 'AcademicSubject' object has no attribute '_prefetch_related_val_userprofile_id'

我正在使用Django 1.7

我的设置方式有什么看起来不对吗?

0 个答案:

没有答案