diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index b35f100fda..543655b357 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -13,7 +13,7 @@ from django.contrib.admin.util import (unquote, flatten_fieldsets, get_deleted_o from django.contrib.admin.templatetags.admin_static import static from django.contrib import messages from django.views.decorators.csrf import csrf_protect -from django.core.exceptions import PermissionDenied, ValidationError +from django.core.exceptions import PermissionDenied, ValidationError, FieldError from django.core.paginator import Paginator from django.core.urlresolvers import reverse from django.db import models, transaction, router @@ -488,7 +488,11 @@ class ModelAdmin(BaseModelAdmin): "formfield_callback": partial(self.formfield_for_dbfield, request=request), } defaults.update(kwargs) - return modelform_factory(self.model, **defaults) + try: + return modelform_factory(self.model, **defaults) + except FieldError as e: + raise FieldError('%s. Check fields/fieldsets/exclude attributes of class %s.' + % (e, self.__class__.__name__)) def get_changelist(self, request, **kwargs): """ diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index a02bb7a316..178df9d844 100644 --- a/django/contrib/admin/validation.py +++ b/django/contrib/admin/validation.py @@ -387,15 +387,6 @@ def check_formfield(cls, model, opts, label, field): except KeyError: raise ImproperlyConfigured("'%s.%s' refers to field '%s' that " "is missing from the form." % (cls.__name__, label, field)) - else: - get_form_is_overridden = hasattr(cls, 'get_form') and cls.get_form != ModelAdmin.get_form - if not get_form_is_overridden: - fields = fields_for_model(model) - try: - fields[field] - except KeyError: - raise ImproperlyConfigured("'%s.%s' refers to field '%s' that " - "is missing from the form." % (cls.__name__, label, field)) def fetch_attr(cls, model, opts, label, field): try: diff --git a/tests/admin_validation/tests.py b/tests/admin_validation/tests.py index 5b2c45f6f2..5329cc7e8c 100644 --- a/tests/admin_validation/tests.py +++ b/tests/admin_validation/tests.py @@ -16,10 +16,6 @@ class ValidFields(admin.ModelAdmin): form = SongForm fields = ['title'] -class InvalidFields(admin.ModelAdmin): - form = SongForm - fields = ['spam'] - class ValidFormFieldsets(admin.ModelAdmin): def get_form(self, request, obj=None, **kwargs): class ExtraFieldForm(SongForm): @@ -49,10 +45,6 @@ class ValidationTestCase(TestCase): # Regression test for #8027: custom ModelForms with fields/fieldsets """ validate(ValidFields, Song) - self.assertRaisesMessage(ImproperlyConfigured, - "'InvalidFields.fields' refers to field 'spam' that is missing from the form.", - validate, - InvalidFields, Song) def test_custom_get_form_with_fieldsets(self): """