Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #8999 -- Removed useless code in handling of `exclude` option i…

…n ModelAdmin and InlineModelAdmin custom form(set) hooks. Thanks goes to seanl for the report, patch and bkonkle and Julien for further work on final patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16422 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b2f5efc6ac59e3341b2d617510137707f214e18f 1 parent b4cdf4d
Ramiro Morales ramiro authored
2  django/contrib/admin/options.py
View
@@ -433,7 +433,6 @@ def get_form(self, request, obj=None, **kwargs):
exclude = []
else:
exclude = list(self.exclude)
- exclude.extend(kwargs.get("exclude", []))
exclude.extend(self.get_readonly_fields(request, obj))
# if exclude is an empty list we pass None to be consistant with the
# default on modelform_factory
@@ -1342,7 +1341,6 @@ def get_formset(self, request, obj=None, **kwargs):
exclude = []
else:
exclude = list(self.exclude)
- exclude.extend(kwargs.get("exclude", []))
exclude.extend(self.get_readonly_fields(request, obj))
# if exclude is an empty list we use None, since that's the actual
# default
57 tests/regressiontests/modeladmin/tests.py
View
@@ -141,6 +141,63 @@ class BandAdmin(ModelAdmin):
type(ma.get_form(request).base_fields['sign_date'].widget),
AdminDateWidget)
+ def test_form_exclude_kwarg_override(self):
+ """
+ Ensure that the `exclude` kwarg passed to `ModelAdmin.get_form()`
+ overrides all other declarations. Refs #8999.
+ """
+
+ class AdminBandForm(forms.ModelForm):
+
+ class Meta:
+ model = Band
+ exclude = ['name']
+
+ class BandAdmin(ModelAdmin):
+ exclude = ['sign_date',]
+ form = AdminBandForm
+
+ def get_form(self, request, obj=None, **kwargs):
+ kwargs['exclude'] = ['bio']
+ return super(BandAdmin, self).get_form(request, obj, **kwargs)
+
+ ma = BandAdmin(Band, self.site)
+ self.assertEqual(ma.get_form(request).base_fields.keys(),
+ ['name', 'sign_date',])
+
+
+ def test_formset_exclude_kwarg_override(self):
+ """
+ Ensure that the `exclude` kwarg passed to `InlineModelAdmin.get_formset()`
+ overrides all other declarations. Refs #8999.
+ """
+
+ class AdminConcertForm(forms.ModelForm):
+
+ class Meta:
+ model = Concert
+ exclude = ['day']
+
+ class ConcertInline(TabularInline):
+ exclude = ['transport']
+ form = AdminConcertForm
+ fk_name = 'main_band'
+ model = Concert
+
+ def get_formset(self, request, obj=None, **kwargs):
+ kwargs['exclude'] = ['opening_band']
+ return super(ConcertInline, self).get_formset(request, obj, **kwargs)
+
+ class BandAdmin(ModelAdmin):
+ inlines = [
+ ConcertInline
+ ]
+
+ ma = BandAdmin(Band, self.site)
+ self.assertEqual(
+ list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
+ ['main_band', 'day', 'transport', 'id', 'DELETE',])
+
def test_queryset_override(self):
# If we need to override the queryset of a ModelChoiceField in our custom form
# make sure that RelatedFieldWidgetWrapper doesn't mess that up.
Please sign in to comment.
Something went wrong with that request. Please try again.