Search code examples
pythondjangodjango-admindjango-users

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


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.

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'].

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.

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.

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.


Solution

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