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

时间:2021-10-02 03:05:12

I'm working on a legacy django 1.4 project, porting it to django 1.7. When accessing the django admin interface, in order to show user data I get the aforementioned error.

我正在研究遗留的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'

The self object in the offending line is a django.forms.models.ModelMultipleChoiceField instance (of course), the formfield in line 333 (see traceback) is instantiated as formfield = self.fields['groups'].

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

The error raises even on an empty db (with just one user). I've checked twice but there are no changes in the AUTH_MODEL, in the signals or any OneToOne link to the User model.

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

I've checked also the external apps for similar issues without any luck. Does anybody had the same issue in the past and can give me an hint on where to search for the source of this error? I'm still reluctant to report a bug to django since I cannot replicate it if I'm unable to define where the error is triggered.

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

EDIT: I've verified that the problem arises in every model that has a foreign key (or a M2M, or a O2O). @Alasdair comment was a good suggestion but for sure I do not alter the queryset in all the models so the reason should be elsewhere.

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

1 个解决方案

#1


1  

I suggest you try disabling simple_autocomplete. There appears to be a similar issue with django-ajax-selects.

我建议你尝试禁用simple_autocomplete。 django-ajax-choices似乎存在类似的问题。

#1


1  

I suggest you try disabling simple_autocomplete. There appears to be a similar issue with django-ajax-selects.

我建议你尝试禁用simple_autocomplete。 django-ajax-choices似乎存在类似的问题。