Removed fragile admin validation of fields on ModelForm

Refs #19445
This commit is contained in:
Luke Plant 2013-05-09 00:49:05 +01:00
parent 832b4a5722
commit 1556b1c3b7
3 changed files with 6 additions and 19 deletions

View File

@ -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.admin.templatetags.admin_static import static
from django.contrib import messages from django.contrib import messages
from django.views.decorators.csrf import csrf_protect 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.paginator import Paginator
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import models, transaction, router from django.db import models, transaction, router
@ -488,7 +488,11 @@ class ModelAdmin(BaseModelAdmin):
"formfield_callback": partial(self.formfield_for_dbfield, request=request), "formfield_callback": partial(self.formfield_for_dbfield, request=request),
} }
defaults.update(kwargs) 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): def get_changelist(self, request, **kwargs):
""" """

View File

@ -387,15 +387,6 @@ def check_formfield(cls, model, opts, label, field):
except KeyError: except KeyError:
raise ImproperlyConfigured("'%s.%s' refers to field '%s' that " raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
"is missing from the form." % (cls.__name__, label, field)) "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): def fetch_attr(cls, model, opts, label, field):
try: try:

View File

@ -16,10 +16,6 @@ class ValidFields(admin.ModelAdmin):
form = SongForm form = SongForm
fields = ['title'] fields = ['title']
class InvalidFields(admin.ModelAdmin):
form = SongForm
fields = ['spam']
class ValidFormFieldsets(admin.ModelAdmin): class ValidFormFieldsets(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs): def get_form(self, request, obj=None, **kwargs):
class ExtraFieldForm(SongForm): class ExtraFieldForm(SongForm):
@ -49,10 +45,6 @@ class ValidationTestCase(TestCase):
# Regression test for #8027: custom ModelForms with fields/fieldsets # Regression test for #8027: custom ModelForms with fields/fieldsets
""" """
validate(ValidFields, Song) 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): def test_custom_get_form_with_fieldsets(self):
""" """