Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored June 16, 2011
2  django/contrib/admin/options.py
@@ -433,7 +433,6 @@ def get_form(self, request, obj=None, **kwargs):
433 433
             exclude = []
434 434
         else:
435 435
             exclude = list(self.exclude)
436  
-        exclude.extend(kwargs.get("exclude", []))
437 436
         exclude.extend(self.get_readonly_fields(request, obj))
438 437
         # if exclude is an empty list we pass None to be consistant with the
439 438
         # default on modelform_factory
@@ -1342,7 +1341,6 @@ def get_formset(self, request, obj=None, **kwargs):
1342 1341
             exclude = []
1343 1342
         else:
1344 1343
             exclude = list(self.exclude)
1345  
-        exclude.extend(kwargs.get("exclude", []))
1346 1344
         exclude.extend(self.get_readonly_fields(request, obj))
1347 1345
         # if exclude is an empty list we use None, since that's the actual
1348 1346
         # default
57  tests/regressiontests/modeladmin/tests.py
@@ -141,6 +141,63 @@ class BandAdmin(ModelAdmin):
141 141
             type(ma.get_form(request).base_fields['sign_date'].widget),
142 142
             AdminDateWidget)
143 143
 
  144
+    def test_form_exclude_kwarg_override(self):
  145
+        """
  146
+        Ensure that the `exclude` kwarg passed to `ModelAdmin.get_form()`
  147
+        overrides all other declarations. Refs #8999.
  148
+        """
  149
+
  150
+        class AdminBandForm(forms.ModelForm):
  151
+
  152
+            class Meta:
  153
+                model = Band
  154
+                exclude = ['name']
  155
+
  156
+        class BandAdmin(ModelAdmin):
  157
+            exclude = ['sign_date',]
  158
+            form = AdminBandForm
  159
+
  160
+            def get_form(self, request, obj=None, **kwargs):
  161
+                kwargs['exclude'] = ['bio']
  162
+                return super(BandAdmin, self).get_form(request, obj, **kwargs)
  163
+
  164
+        ma = BandAdmin(Band, self.site)
  165
+        self.assertEqual(ma.get_form(request).base_fields.keys(),
  166
+            ['name', 'sign_date',])
  167
+
  168
+
  169
+    def test_formset_exclude_kwarg_override(self):
  170
+        """
  171
+        Ensure that the `exclude` kwarg passed to `InlineModelAdmin.get_formset()`
  172
+        overrides all other declarations. Refs #8999.
  173
+        """
  174
+
  175
+        class AdminConcertForm(forms.ModelForm):
  176
+
  177
+            class Meta:
  178
+                model = Concert
  179
+                exclude = ['day']
  180
+
  181
+        class ConcertInline(TabularInline):
  182
+            exclude = ['transport']
  183
+            form = AdminConcertForm
  184
+            fk_name = 'main_band'
  185
+            model = Concert
  186
+
  187
+            def get_formset(self, request, obj=None, **kwargs):
  188
+                kwargs['exclude'] = ['opening_band']
  189
+                return super(ConcertInline, self).get_formset(request, obj, **kwargs)
  190
+
  191
+        class BandAdmin(ModelAdmin):
  192
+            inlines = [
  193
+                ConcertInline
  194
+            ]
  195
+
  196
+        ma = BandAdmin(Band, self.site)
  197
+        self.assertEqual(
  198
+            list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
  199
+            ['main_band', 'day', 'transport', 'id', 'DELETE',])
  200
+
144 201
     def test_queryset_override(self):
145 202
         # If we need to override the queryset of a ModelChoiceField in our custom form
146 203
         # make sure that RelatedFieldWidgetWrapper doesn't mess that up.

0 notes on commit b2f5efc

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