Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19445 -- Skip admin fieldsets validation when the Mode…

…lAdmin.get_form() method is overridden.

Backport of 0694d21
  • Loading branch information...
commit e18bd68dbc64dfe08ae0fbb54591d5c8f7e456eb 1 parent f28fbe7
Nick Sandford authored February 02, 2013 jphalip committed February 02, 2013
16  django/contrib/admin/validation.py
@@ -6,7 +6,7 @@
6 6
 from django.contrib.admin import ListFilter, FieldListFilter
7 7
 from django.contrib.admin.util import get_fields_from_path, NotRelationField
8 8
 from django.contrib.admin.options import (flatten_fieldsets, BaseModelAdmin,
9  
-    HORIZONTAL, VERTICAL)
  9
+    ModelAdmin, HORIZONTAL, VERTICAL)
10 10
 
11 11
 
12 12
 __all__ = ['validate']
@@ -388,12 +388,14 @@ def check_formfield(cls, model, opts, label, field):
388 388
             raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
389 389
                 "is missing from the form." % (cls.__name__, label, field))
390 390
     else:
391  
-        fields = fields_for_model(model)
392  
-        try:
393  
-            fields[field]
394  
-        except KeyError:
395  
-            raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
396  
-                "is missing from the form." % (cls.__name__, label, field))
  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))
397 399
 
398 400
 def fetch_attr(cls, model, opts, label, field):
399 401
     try:
7  docs/ref/contrib/admin/index.txt
@@ -1096,6 +1096,13 @@ templates used by the :class:`ModelAdmin` views:
1096 1096
     changelist that will be linked to the change view, as described in the
1097 1097
     :attr:`ModelAdmin.list_display_links` section.
1098 1098
 
  1099
+.. method:: ModelAdmin.get_fieldsets(self, request, obj=None)
  1100
+
  1101
+    The ``get_fieldsets`` method is given the ``HttpRequest`` and the ``obj``
  1102
+    being edited (or ``None`` on an add form) and is expected to return a list
  1103
+    of two-tuples, in which each two-tuple represents a ``<fieldset>`` on the
  1104
+    admin form page, as described above in the :attr:`ModelAdmin.fieldsets` section.
  1105
+
1099 1106
 .. method:: ModelAdmin.get_list_filter(self, request)
1100 1107
 
1101 1108
     .. versionadded:: 1.5
20  tests/regressiontests/admin_validation/tests.py
@@ -20,6 +20,18 @@ class InvalidFields(admin.ModelAdmin):
20 20
     form = SongForm
21 21
     fields = ['spam']
22 22
 
  23
+class ValidFormFieldsets(admin.ModelAdmin):
  24
+    def get_form(self, request, obj=None, **kwargs):
  25
+        class ExtraFieldForm(SongForm):
  26
+            name = forms.CharField(max_length=50)
  27
+        return ExtraFieldForm
  28
+
  29
+    fieldsets = (
  30
+        (None, {
  31
+            'fields': ('name',),
  32
+        }),
  33
+    )
  34
+
23 35
 class ValidationTestCase(TestCase):
24 36
 
25 37
     def test_readonly_and_editable(self):
@@ -42,6 +54,14 @@ def test_custom_modelforms_with_fields_fieldsets(self):
42 54
             validate,
43 55
             InvalidFields, Song)
44 56
 
  57
+    def test_custom_get_form_with_fieldsets(self):
  58
+        """
  59
+        Ensure that the fieldsets validation is skipped when the ModelAdmin.get_form() method
  60
+        is overridden.
  61
+        Refs #19445.
  62
+        """
  63
+        validate(ValidFormFieldsets, Song)
  64
+
45 65
     def test_exclude_values(self):
46 66
         """
47 67
         Tests for basic validation of 'exclude' option values (#12689)

0 notes on commit e18bd68

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