Code

Opened 5 years ago

Closed 4 years ago

#10263 closed (fixed)

order_by parameter for inlineformset_factory and BaseInlineFormSet

Reported by: Paulo Scardine <paulo@…> Owned by: nobody
Component: Forms Version: 1.0
Severity: Keywords: inlineformset_factory BaseInlineFormSet inline model formset
Cc: andy@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Problem

At inlineformset_factory and BaseInlineFormSet constructor there is no option to specify order_by criteria for the resulting queryset.

The record order at the rendered formset will depend on the order returned by the database layer; notably PostgreSQL may change this order after every update. When using PRG (Post/Redirect/Get) the result is unpleasant and may confuse users.

Proposed Solution

I would like to add an order_by parameter to inlineformset_factory and BaseInlineFormSet.

Attachments (4)

inlineformset_factory_order_by.diff (2.3 KB) - added by Paulo Scardine <paulo@…> 5 years ago.
Patch to add order_by parameter for inlineformset_factory
inline_formset_queryset_argument.diff (5.6 KB) - added by paulos 5 years ago.
New patch as sugested by Alex
inline_formset_queryset_argument_2.diff (5.6 KB) - added by paulos 5 years ago.
Chained the fk filter even if queryset provided, as sugested by Alex.
inlineformset_queryset_parameter.diff (5.5 KB) - added by paulos 5 years ago.

Download all attachments as: .zip

Change History (12)

Changed 5 years ago by Paulo Scardine <paulo@…>

Patch to add order_by parameter for inlineformset_factory

comment:1 Changed 5 years ago by carljm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Related: #9758, #9006.

comment:2 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Design decision needed

marking as design decision needed, why not just pass the queryset already ordered?

comment:3 Changed 5 years ago by anonymous

At first it also looked like a good idea to pass a queryset ready.

But giving it some thought to this issue, my opinion changed. I think the purpose of inlineformset_factory is very specific: making a formset linked by the parent_model PK.

So what is the point of passing a queryset as argument? If it is for flexibility, it seems out of place, because we should address the common use case and not the exception.

If you could make the point for a broader solution I will be happy to provide a patch.

Changed 5 years ago by paulos

New patch as sugested by Alex

comment:4 Changed 5 years ago by paulos

New patch adds a queryset argument to BaseInlineFormSet making it consistent with BaseModelFormset, as sugested by Alex.

Changed 5 years ago by paulos

Chained the fk filter even if queryset provided, as sugested by Alex.

Changed 5 years ago by paulos

comment:5 Changed 5 years ago by paulos

Last patch is a little better styled (won't trigger evaluation).

comment:6 Changed 5 years ago by Alex

  • Triage Stage changed from Design decision needed to Accepted

comment:7 Changed 5 years ago by anonymous

  • Cc andy@… added

comment:8 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [11874]) Fixed #10263 -- Added a queryset argument to BaseInlineFormSet, normalizing with BaseModelFormSet, and allowing for custom ordering/subsets in inlines. Thanks to Paulo Scardine for the patch.

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.