#15820: inline admin formset uses incorrect form
---------------------------------------+------------------------------
 Reporter:  shanyu                     |         Owner:  nobody
     Type:  Bug                        |        Status:  new
Milestone:                             |     Component:  contrib.admin
  Version:  1.2                        |      Severity:  Normal
 Keywords:  inlinemodeladmin, inlines  |  Triage Stage:  Unreviewed
Has patch:  0                          |
---------------------------------------+------------------------------
 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".

-- 
Ticket URL: <http://code.djangoproject.com/ticket/15820>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to