#11616 closed (duplicate)
Ability add filter to inlineformset_factory
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | |
Severity: | Keywords: | inlineformset_factory | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When using inlineformset_factory it would be helpful if an optional filter can be provided to limit the rows returned into the inline formset.
In some situations you want to limit the data to be edited in the form to a subset of the header record.
The example below limits the rows on the inline formset only to lines were the status is open.
mainform = Form(instance=data) formset1 = InlineFormSet1(instance=data, prefix="inline1", filter="status='open'")
As the formset is built, in addition to the foreign key set to the instance id the queryset (qs) is also limited to whatever is provided in filter.
Addition looks a little like below (plus some more code).
# __init__ in class BaseInlineFormset qs = self.model._default_manager.filter(**{self.fk.name: self.instance}, filter)
Marking as a dupe of #10263 since it's solution is sufficiently general to solve this issue.