django 1.7 admin:'ModelMultipleChoiceField'对象没有属性'limit_choices_to'

时间:2015-02-17 19:26:23

标签: python django django-admin django-users

我正在研究传统的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']。< / p>

即使在空数据库(仅有一个用户)上也会出现错误。我已经检查了两次,但信号中的AUTH_MODELUser模型的任何OneToOne链接都没有变化。

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

修改 我已经验证了每个具有外键(或M2M或O2O)的模型都会出现问题。 @Alasdair评论是一个很好的建议,但我确实不会改变所有模型中的查询集,所以原因应该在其他地方。

1 个答案:

答案 0 :(得分:1)

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