Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed fragile admin validation of fields on ModelForm

Refs #19445
  • Loading branch information...
commit 1556b1c3b7a18cadf4d66d2ab6301d7fe8646ba5 1 parent 832b4a5
@spookylukey spookylukey authored
View
8 django/contrib/admin/options.py
@@ -13,7 +13,7 @@
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 @@ def get_form(self, request, obj=None, **kwargs):
"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):
"""
View
9 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:
View
8 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 @@ def test_custom_modelforms_with_fields_fieldsets(self):
# 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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.