Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#11616 closed (duplicate)

Ability add filter to inlineformset_factory

Reported by: Marcel van Eck <vaneck.marcel@…> 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


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.instance}, filter)

Change History (2)

comment:1 Changed 10 years ago by Alex Gaynor

Resolution: duplicate
Status: newclosed

Marking as a dupe of #10263 since it's solution is sufficiently general to solve this issue.

comment:2 Changed 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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