Ticket #15820: 15820.inlinemodeladmin-get_fieldsets.diff

File 15820.inlinemodeladmin-get_fieldsets.diff, 2.4 KB (added by Julien Phalip, 13 years ago)

Fix + tests

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index c603210..ef600de 100644
    a b class InlineModelAdmin(BaseModelAdmin):  
    13501350    def get_fieldsets(self, request, obj=None):
    13511351        if self.declared_fieldsets:
    13521352            return self.declared_fieldsets
    1353         form = self.get_formset(request).form
     1353        form = self.get_formset(request, obj).form
    13541354        fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
    13551355        return [(None, {'fields': fields})]
    13561356
  • tests/regressiontests/modeladmin/tests.py

    diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py
    index 530b476..3faf18c 100644
    a b class ModelAdminTests(TestCase):  
    180180            '<option value="%d">The Doors</option>\n'
    181181            '</select>' % self.band.id)
    182182
     183    def test_regression_for_ticket_15820(self):
     184        """
     185        Ensure that `obj` is passed from `InlineModelAdmin.get_fieldsets()` to
     186        `InlineModelAdmin.get_formset()`.
     187        """
     188        class CustomConcertForm(forms.ModelForm):
     189           
     190            class Meta:
     191                model = Concert
     192                fields = ['day']
     193
     194        class ConcertInline(TabularInline):
     195            model = Concert
     196            fk_name = 'main_band'
     197
     198            def get_formset(self, request, obj=None, **kwargs):
     199                if obj:
     200                    kwargs['form'] = CustomConcertForm
     201                return super(ConcertInline, self).get_formset(request, obj, **kwargs)
     202           
     203        class BandAdmin(ModelAdmin):
     204            inlines = [
     205                ConcertInline
     206            ]
     207
     208        concert = Concert.objects.create(main_band=self.band, opening_band=self.band, day=1)
     209        ma = BandAdmin(Band, self.site)
     210        fieldsets = list(ma.inline_instances[0].get_fieldsets(request))
     211        self.assertEqual(fieldsets[0][1]['fields'], ['main_band', 'opening_band', 'day', 'transport'])
     212        fieldsets = list(ma.inline_instances[0].get_fieldsets(request, ma.inline_instances[0].model))
     213        self.assertEqual(fieldsets[0][1]['fields'], ['day'])
     214       
    183215    # radio_fields behavior ###########################################
    184216
    185217    def test_default_foreign_key_widget(self):
Back to Top