django 1.7 管理员:'ModelMultipleChoiceField' 对象没有属性 'limit_choices_to'

django 1.7 admin: 'ModelMultipleChoiceField' object has no attribute 'limit_choices_to'

我正在处理遗留的 django 1.4 项目,将其移植到 django 1.7。 当访问 django 管理界面时,为了显示用户数据,我得到了上述错误。

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/auth/user/1/

Django Version: 1.7.4
Python Version: 2.7.8

Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.humanize',
 'simple_autocomplete',
 'paypal.standard.ipn',
 'sorl.thumbnail',
 'haystack',
 'crispy_forms',
 'siteprefs',
 'sitegate',
 'debug_toolbar',
 'modeltranslation',
 'rosetta',
# omitted: some project-specific apps
)

    Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Traceback:
File "MYENV/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "MYENV/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  583.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  206.             return view(request, *args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/contrib/admin/options.py" in change_view
  1456.         return self.changeform_view(request, object_id, form_url, extra_context)
File "MYENV/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "MYENV/lib/python2.7/site-packages/django/db/transaction.py" in inner
  394.                 return func(*args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/contrib/admin/options.py" in changeform_view
  1419.                 form = ModelForm(instance=obj)
File "MYENV/lib/python2.7/site-packages/django/contrib/auth/forms.py" in __init__
  145.         super(UserChangeForm, self).__init__(*args, **kwargs)
File "MYENV/lib/python2.7/site-packages/django/forms/models.py" in __init__
  333.                 limit_choices_to = formfield.get_limit_choices_to()
File "MYENV/lib/python2.7/site-packages/django/forms/models.py" in get_limit_choices_to
  1126.         if callable(self.limit_choices_to):

Exception Type: AttributeError at /admin/auth/user/1/
Exception Value: 'ModelMultipleChoiceField' object has no attribute 'limit_choices_to'

违规行中的 self 对象是一个 django.forms.models.ModelMultipleChoiceField 实例(当然),第 333 行中的 formfield (参见回溯)被实例化为 formfield = self.fields['groups'] .

即使在空数据库(只有一个用户)上也会出现错误。我检查了两次,但 AUTH_MODEL、信号或 User 模型的任何 OneToOne link 都没有变化。

我也检查了外部应用程序是否存在类似问题,但没有任何运气。过去有没有人遇到过同样的问题,可以提示我在哪里搜索此错误的来源?我仍然不愿意向 django 报告错误,因为如果我无法定义触发错误的位置,我将无法复制它。

编辑: 我已经确认问题出现在每个具有外键(或 M2M 或 O2O)的模型中。 @Alasdair 评论是一个很好的建议,但我肯定不会更改所有模型中的查询集,所以原因应该在别处。

我建议您尝试禁用 simple_autocomplete。 django-ajax-selects 似乎有 a similar issue