Opened 6 years ago

Closed 6 years ago

Last modified 4 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: UI/UX:

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)

Change History (2)

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

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

comment:2 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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