Code

Opened 5 years ago

Closed 5 years ago

Last modified 3 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)

Attachments (0)

Change History (2)

comment:1 Changed 5 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 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.