2

我有一个与第二个模型具有多对多关系的模型。当我尝试使用 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)

(各种细节省略)。TranslatableModeland来自django-hvadTranslatedFields ,虽然我不认为它们在这种情况下很重要,但我不是 100% 确定,所以我想我会包括在内。

我试图遍历的查询集是:

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

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

4

0 回答 0