Opened 9 years ago
Last modified 5 years ago
#25880 assigned Cleanup/optimization
Forms created by formset_factory's extra parameter don't have instance set
Reported by: | karyon | Owned by: | Parth Patil |
---|---|---|---|
Component: | Forms | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | johannes.linke@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
i have code of the following form:
Model A: pass Model B: related_field = ForeignKey(A) class BForm(ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) print(self.instance.related_field) def some_view(request): InlineBFormset = inlineformset_factory(A, B, formset=ABFormset, form=BForm, extra=1) formset = InlineBFormset(request.POST or None, instance=some_a_instance)
the line with the print crashes for the form created by the extra=1 with RelatedObjectDoesNotExist: B has no A. i would have expected that the some_a_instance is set on the form's self.instance.
in django 1.9, i can easily work around that by handing over constructor parameters, but it would still seem weird to me to hand over the related instance that would later be set by the formset's save method anyways.
i can provide the real code if needed.
Change History (3)
comment:1 by , 9 years ago
Cc: | added |
---|
follow-up: 3 comment:2 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Bug → Cleanup/optimization |
comment:3 by , 5 years ago
Needs documentation: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Replying to Tim Graham:
Conceptually it seems to make sense, though from a look at the code I couldn't tell if it's feasible. I guess I'll accept the ticket for further investigation. BaseInlineFormSet._construct_form() seems to set the related instance after the form is initialized. If not, maybe it's worth a documentation note?
Yes, I agree with Tim. Since _construct_form()
method does not assign the related object, I think it should be added and document note should also be present for earlier versions
Conceptually it seems to make sense, though from a look at the code I couldn't tell if it's feasible. I guess I'll accept the ticket for further investigation. BaseInlineFormSet._construct_form() seems to set the related instance after the form is initialized. If not, maybe it's worth a documentation note?