Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #19445 - Fieldsets' validation doesn't call get_form #636

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions django/contrib/admin/validation.py
Expand Up @@ -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']
Expand Down Expand Up @@ -387,7 +387,7 @@ 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:
elif hasattr(cls, 'get_form') and cls.get_form.__func__ is ModelAdmin.get_form.__func__:
fields = fields_for_model(model)
try:
fields[field]
Expand Down
7 changes: 7 additions & 0 deletions docs/ref/contrib/admin/index.txt
Expand Up @@ -1049,6 +1049,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
Expand Down
15 changes: 15 additions & 0 deletions tests/regressiontests/admin_validation/tests.py
Expand Up @@ -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):
Expand All @@ -42,6 +54,9 @@ def test_custom_modelforms_with_fields_fieldsets(self):
validate,
InvalidFields, Song)

def test_custom_get_form_with_fieldsets(self):
validate(ValidFormFieldsets, Song)

def test_exclude_values(self):
"""
Tests for basic validation of 'exclude' option values (#12689)
Expand Down