Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15820 -- Fixed the use of the InlineAdmin's get_formset method…

… by actually passing in the obj. Thanks, Julien Phalip.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16497 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f9fe112f2d65ea187ad47b280197664b551f339e 1 parent 43cb78f
Jannis Leidel authored
2  django/contrib/admin/options.py
@@ -1360,7 +1360,7 @@ def get_formset(self, request, obj=None, **kwargs):
1360 1360
     def get_fieldsets(self, request, obj=None):
1361 1361
         if self.declared_fieldsets:
1362 1362
             return self.declared_fieldsets
1363  
-        form = self.get_formset(request).form
  1363
+        form = self.get_formset(request, obj).form
1364 1364
         fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
1365 1365
         return [(None, {'fields': fields})]
1366 1366
 
32  tests/regressiontests/modeladmin/tests.py
@@ -237,6 +237,38 @@ class ConcertAdmin(ModelAdmin):
237 237
             '<option value="%d">The Doors</option>\n'
238 238
             '</select>' % self.band.id)
239 239
 
  240
+    def test_regression_for_ticket_15820(self):
  241
+        """
  242
+        Ensure that `obj` is passed from `InlineModelAdmin.get_fieldsets()` to
  243
+        `InlineModelAdmin.get_formset()`.
  244
+        """
  245
+        class CustomConcertForm(forms.ModelForm):
  246
+
  247
+            class Meta:
  248
+                model = Concert
  249
+                fields = ['day']
  250
+
  251
+        class ConcertInline(TabularInline):
  252
+            model = Concert
  253
+            fk_name = 'main_band'
  254
+
  255
+            def get_formset(self, request, obj=None, **kwargs):
  256
+                if obj:
  257
+                    kwargs['form'] = CustomConcertForm
  258
+                return super(ConcertInline, self).get_formset(request, obj, **kwargs)
  259
+
  260
+        class BandAdmin(ModelAdmin):
  261
+            inlines = [
  262
+                ConcertInline
  263
+            ]
  264
+
  265
+        concert = Concert.objects.create(main_band=self.band, opening_band=self.band, day=1)
  266
+        ma = BandAdmin(Band, self.site)
  267
+        fieldsets = list(ma.inline_instances[0].get_fieldsets(request))
  268
+        self.assertEqual(fieldsets[0][1]['fields'], ['main_band', 'opening_band', 'day', 'transport'])
  269
+        fieldsets = list(ma.inline_instances[0].get_fieldsets(request, ma.inline_instances[0].model))
  270
+        self.assertEqual(fieldsets[0][1]['fields'], ['day'])
  271
+
240 272
     # radio_fields behavior ###########################################
241 273
 
242 274
     def test_default_foreign_key_widget(self):

0 notes on commit f9fe112

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