Opened 9 years ago

Closed 9 years ago

#15820 closed Bug (fixed)

inline admin formset uses incorrect form

Reported by: shanyu Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Normal Keywords: inlinemodeladmin, inlines
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have the (somewhat simplified) following code:

def create_foo_form(baz):
    class FooForm(forms.ModelForm):
       def __init__(self, *args, **kwargs):
           print "Initializing a FooForm object"
           # Init stuff here
    return FooForm

class FooInline(admin.TabularInline):
    model = Foo

    def get_formset(self, request, obj=None, **kwargs):
        if obj:
            kwargs['form'] = create_foo_form(obj)
        return super(FooInline, self).get_formset(request, obj, **kwargs)

class BazAdmin(admin.ModelAdmin):
    inlines = [FooInline,]

The problem is that the inline formset always uses my custom FooForm, though the code above clearly chooses it only if obj is not None (Otherwise the default self.form should be used).

The following code is get_fieldsets method of InlineModelAdmin.

    def get_fieldsets(self, request, obj=None):
        if self.declared_fieldsets:
            return self.declared_fieldsets
        form = self.get_formset(request).form
        fields = form.base_fields.keys() + list(self.get_readonly_fields(request, obj))
        return [(None, {'fields': fields})]

The line "form = self.get_formset(request).form" calls get_formset method without providing the argument "obj". That results in using an incorrect form in this case. I guess the correct form would be "form = self.get_formset(request, obj).form".

Attachments (1)

15820.inlinemodeladmin-get_fieldsets.diff (2.4 KB) - added by Julien Phalip 9 years ago.
Fix + tests

Download all attachments as: .zip

Change History (5)

comment:1 Changed 9 years ago by Jacob

Easy pickings: unset
Triage Stage: UnreviewedAccepted

Changed 9 years ago by Julien Phalip

Fix + tests

comment:2 Changed 9 years ago by Julien Phalip

Has patch: set

comment:3 Changed 9 years ago by Preston Timmons

Triage Stage: AcceptedReady for checkin
UI/UX: unset

I applied and reviewed this patch for r16452. It looks good. Marking as RFC.

comment:4 Changed 9 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

In [16497]:

(The changeset message doesn't reference this ticket)

Note: See TracTickets for help on using tickets.
Back to Top