Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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 slurms authored jphalip committed
16 django/contrib/admin/validation.py
View
@@ -6,7 +6,7 @@
from django.contrib.admin import ListFilter, FieldListFilter
from django.contrib.admin.util import get_fields_from_path, NotRelationField
from django.contrib.admin.options import (flatten_fieldsets, BaseModelAdmin,
- HORIZONTAL, VERTICAL)
+ ModelAdmin, HORIZONTAL, VERTICAL)
__all__ = ['validate']
@@ -388,12 +388,14 @@ def check_formfield(cls, model, opts, label, field):
raise ImproperlyConfigured("'%s.%s' refers to field '%s' that "
"is missing from the form." % (cls.__name__, label, field))
else:
- 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))
+ 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:
7 docs/ref/contrib/admin/index.txt
View
@@ -1096,6 +1096,13 @@ templates used by the :class:`ModelAdmin` views:
changelist that will be linked to the change view, as described in the
:attr:`ModelAdmin.list_display_links` section.
+.. method:: ModelAdmin.get_fieldsets(self, request, obj=None)
+
+ The ``get_fieldsets`` method is given the ``HttpRequest`` and the ``obj``
+ being edited (or ``None`` on an add form) and is expected to return a list
+ of two-tuples, in which each two-tuple represents a ``<fieldset>`` on the
+ admin form page, as described above in the :attr:`ModelAdmin.fieldsets` section.
+
.. method:: ModelAdmin.get_list_filter(self, request)
.. versionadded:: 1.5
20 tests/regressiontests/admin_validation/tests.py
View
@@ -20,6 +20,18 @@ class InvalidFields(admin.ModelAdmin):
form = SongForm
fields = ['spam']
+class ValidFormFieldsets(admin.ModelAdmin):
+ def get_form(self, request, obj=None, **kwargs):
+ class ExtraFieldForm(SongForm):
+ name = forms.CharField(max_length=50)
+ return ExtraFieldForm
+
+ fieldsets = (
+ (None, {
+ 'fields': ('name',),
+ }),
+ )
+
class ValidationTestCase(TestCase):
def test_readonly_and_editable(self):
@@ -42,6 +54,14 @@ def test_custom_modelforms_with_fields_fieldsets(self):
validate,
InvalidFields, Song)
+ def test_custom_get_form_with_fieldsets(self):
+ """
+ Ensure that the fieldsets validation is skipped when the ModelAdmin.get_form() method
+ is overridden.
+ Refs #19445.
+ """
+ validate(ValidFormFieldsets, Song)
+
def test_exclude_values(self):
"""
Tests for basic validation of 'exclude' option values (#12689)
Please sign in to comment.
Something went wrong with that request. Please try again.