[Django] #35843: Changing the rendering order of formsets is not clear

21 views
Skip to first unread message

Django

unread,
Oct 15, 2024, 5:51:31 PM10/15/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Type:
| Cleanup/optimization
Status: new | Component:
| Documentation
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I have a formset that I'd like to modify the order of the forms in. I did
overwrite the `forms` `cached_property` with a simple sorted addition to
the base `forms` `cached_property`, but I soon discovered that data wasn't
saving properly in some cases.

Apparently, I went about it all wrong. There's this little tiny tidbit
here about rendering forms:
[https://docs.djangoproject.com/en/5.1/topics/forms/formsets/#:~:text=Iterating%20over%20a%20formset%20will,which%20returns%20the%20corresponding%20form]

{{{
Iterating over a formset will render the forms in the order they were
created. You can change this order by providing an alternate
implementation for the __iter__() method.

Formsets can also be indexed into, which returns the corresponding form.
If you override __iter__, you will need to also override __getitem__ to
have matching behavior.
}}}

Okay, but I've never implemented an `__iter__` method, and based on what
I've read, you also have to implement a `__next__` method. And you
explicitly tell me to implement the `__getitem__` method. I understand if
you don't want to make changing the rendering ordering easier to implement
in Django; however, I would appreciate more and better documentation
including an example of how exactly to implement any of these methods to
change the rendering order of a formset.
--
Ticket URL: <https://code.djangoproject.com/ticket/35843>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 15, 2024, 6:07:02 PM10/15/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Matthew Pava):

I was able to get it to work by basically putting the `sorted` expression
into the `__iter__` method and removing my implementation of `forms`.

{{{
def __iter__(self):
return (i for i in sorted([f for f in self.forms],
key=self._sort_form_key, reverse=True))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:1>

Django

unread,
Oct 16, 2024, 4:06:00 AM10/16/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
--------------------------------------+------------------------------------
Reporter: Matthew Pava | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* component: Documentation => Forms
* stage: Unreviewed => Accepted

Comment:

I agree an example would be nice
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:2>

Django

unread,
Oct 16, 2024, 4:33:00 AM10/16/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
--------------------------------------+------------------------------------
Reporter: Matthew Pava | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Clifford Gama):

I'm not sure that we need to include an example, since the iterator
protocol is Python and not Django, and is documented in python docs
[https://docs.python.org/3/tutorial/classes.html#iterators here], and
emulating container types is documented
[https://docs.python.org/3/reference/datamodel.html#emulating-container-
types here]. Considering that, maybe a link to the docs or to an excellent
tutorial may suffice, or we risk straying out of the scope Django docs and
into into core Python fundamentals.
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:3>

Django

unread,
Oct 16, 2024, 4:45:18 AM10/16/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
--------------------------------------+------------------------------------
Reporter: Matthew Pava | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Sarah Boyce):

I agree with you Clifford, but I was also quite tempted that we remove
those two sentences entirely and reduce it just to:
`Iterating over a formset will render the forms in the order they were
created.`

I'm not sure how much value we add by stating you can update the order of
iterating by overriding the iterator (this should be the case for most
things).
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:4>

Django

unread,
Oct 16, 2024, 5:34:58 AM10/16/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
--------------------------------------+------------------------------------
Reporter: Matthew Pava | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Clifford Gama):

I think we can keep the sentences. This ticket shows that someone needs to
reorder formsets, and pointing them to what they'd need to do to
accomplish that is helpful. Maybe we can change the sentences so that we
don't mention the specific methods`__iter__`, `__get_item__` and
`__next__`: something like: "you can change the order by overriding the
iterator protocol" with a link the relevant Python docs. Or maybe keep the
methods, but make them link to their own docs.
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:5>

Django

unread,
Oct 16, 2024, 8:58:39 AM10/16/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
--------------------------------------+------------------------------------
Reporter: Matthew Pava | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Natalia Bidart):

I also think that keeping the sentence helps since I wouldn't have
considered overriding `__iter__` to control the formset ordering. I agree
with Clifford that we should say something along the line "this is really
implementing the methods that define an object as an iterable". For
example pointing to https://docs.python.org/3/glossary.html#term-iterable,
but please note that formsets provide an iterable API (which is slightly
different from an iterator API: the former implement `__iter__` and
`__getitem__`, and the latter `__iter__` and `__next__`).
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:6>

Django

unread,
Oct 18, 2024, 5:36:25 AM10/18/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* has_patch: 0 => 1
* owner: (none) => Clifford Gama
* status: new => assigned

Comment:

[https://github.com/django/django/pull/18690 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:7>

Django

unread,
Nov 13, 2024, 12:54:27 PM11/13/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Forms | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:8>

Django

unread,
Nov 13, 2024, 1:14:28 PM11/13/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Forms | Version: 5.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"299b072498b23d1d7fe9f1545f7b27b73ca8e22b" 299b072]:
{{{#!CommitTicketReference repository=""
revision="299b072498b23d1d7fe9f1545f7b27b73ca8e22b"
Fixed #35843 -- Clarified formset docs about reordering forms.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:9>

Django

unread,
Nov 13, 2024, 1:15:58 PM11/13/24
to django-...@googlegroups.com
#35843: Changing the rendering order of formsets is not clear
-------------------------------------+-------------------------------------
Reporter: Matthew Pava | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Forms | Version: 5.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia <124304+nessita@…>):

In [changeset:"4c5455d25ccd3b6227c8f3e58406b12a1fb703c7" 4c5455d2]:
{{{#!CommitTicketReference repository=""
revision="4c5455d25ccd3b6227c8f3e58406b12a1fb703c7"
[5.1.x] Fixed #35843 -- Clarified formset docs about reordering forms.

Backport of 299b072498b23d1d7fe9f1545f7b27b73ca8e22b from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35843#comment:10>
Reply all
Reply to author
Forward
0 new messages