Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed fragile admin validation of fields on ModelForm

Refs #19445
  • Loading branch information...
commit 1556b1c3b7a18cadf4d66d2ab6301d7fe8646ba5 1 parent 832b4a5
Luke Plant authored May 09, 2013
8  django/contrib/admin/options.py
@@ -13,7 +13,7 @@
13 13
 from django.contrib.admin.templatetags.admin_static import static
14 14
 from django.contrib import messages
15 15
 from django.views.decorators.csrf import csrf_protect
16  
-from django.core.exceptions import PermissionDenied, ValidationError
  16
+from django.core.exceptions import PermissionDenied, ValidationError, FieldError
17 17
 from django.core.paginator import Paginator
18 18
 from django.core.urlresolvers import reverse
19 19
 from django.db import models, transaction, router
@@ -488,7 +488,11 @@ def get_form(self, request, obj=None, **kwargs):
488 488
             "formfield_callback": partial(self.formfield_for_dbfield, request=request),
489 489
         }
490 490
         defaults.update(kwargs)
491  
-        return modelform_factory(self.model, **defaults)
  491
+        try:
  492
+            return modelform_factory(self.model, **defaults)
  493
+        except FieldError as e:
  494
+            raise FieldError('%s. Check fields/fieldsets/exclude attributes of class %s.'
  495
+                             % (e, self.__class__.__name__))
492 496
 
493 497
     def get_changelist(self, request, **kwargs):
494 498
         """
9  django/contrib/admin/validation.py
@@ -387,15 +387,6 @@ def check_formfield(cls, model, opts, label, field):
387 387
         except KeyError:
388 388
             raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
389 389
                 "is missing from the form." % (cls.__name__, label, field))
390  
-    else:
391  
-        get_form_is_overridden = hasattr(cls, 'get_form') and cls.get_form != ModelAdmin.get_form
392  
-        if not get_form_is_overridden:
393  
-            fields = fields_for_model(model)
394  
-            try:
395  
-                fields[field]
396  
-            except KeyError:
397  
-                raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
398  
-                    "is missing from the form." % (cls.__name__, label, field))
399 390
 
400 391
 def fetch_attr(cls, model, opts, label, field):
401 392
     try:
8  tests/admin_validation/tests.py
@@ -16,10 +16,6 @@ class ValidFields(admin.ModelAdmin):
16 16
     form = SongForm
17 17
     fields = ['title']
18 18
 
19  
-class InvalidFields(admin.ModelAdmin):
20  
-    form = SongForm
21  
-    fields = ['spam']
22  
-
23 19
 class ValidFormFieldsets(admin.ModelAdmin):
24 20
     def get_form(self, request, obj=None, **kwargs):
25 21
         class ExtraFieldForm(SongForm):
@@ -49,10 +45,6 @@ def test_custom_modelforms_with_fields_fieldsets(self):
49 45
         # Regression test for #8027: custom ModelForms with fields/fieldsets
50 46
         """
51 47
         validate(ValidFields, Song)
52  
-        self.assertRaisesMessage(ImproperlyConfigured,
53  
-            "'InvalidFields.fields' refers to field 'spam' that is missing from the form.",
54  
-            validate,
55  
-            InvalidFields, Song)
56 48
 
57 49
     def test_custom_get_form_with_fieldsets(self):
58 50
         """

0 notes on commit 1556b1c

Please sign in to comment.
Something went wrong with that request. Please try again.