Opened 15 years ago
Closed 14 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 |
Description
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)
Change History (5)
comment:1 by , 15 years ago
| Easy pickings: | unset |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
by , 14 years ago
| Attachment: | 15820.inlinemodeladmin-get_fieldsets.diff added |
|---|
comment:2 by , 14 years ago
| Has patch: | set |
|---|
comment:3 by , 14 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|---|
| UI/UX: | unset |
I applied and reviewed this patch for r16452. It looks good. Marking as RFC.
comment:4 by , 14 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
In [16497]:
(The changeset message doesn't reference this ticket)
Fix + tests