Form fields - should be OrderedDict?

85 views
Skip to first unread message

אורי

unread,
Apr 11, 2020, 12:52:45 AM4/11/20
to Django developers (Contributions to Django itself)
Django developers,

When trying to upgrade Django from 2.2 to 3.0, I encountered a problem because form fields were changed from OrderedDict to a dict. I asked on Stack Overflow and got an answer, which was to change the definition of self.fields in my form to OrderedDict ( https://stackoverflow.com/questions/61140458/how-do-i-specify-order-of-fields-in-django-form ). So I'm asking - why not change form fields to OrderedDict? Why was it changed to dict in the first place? Is there any advantage of using dict? I think changing to dict (from OrderedDict) in Django 3.0 is a mistake and it should be changed back to OrderedDict.

Thanks,
Uri.

James Bennett

unread,
Apr 11, 2020, 1:03:57 AM4/11/20
to django-d...@googlegroups.com
The purpose of OrderedDict is that it's a dictionary type where
iteration yields keys in the order they were inserted. In older
versions of Python, this was not guaranteed, so a special ordered
version was needed.

Django 3.0 supports only Python 3.6 and newer; as of Python 3.6,
iterating a normal dict yields the keys in insertion order (and this
is now a guaranteed part of the Python language specification). So
there is no longer any reason, in Django 3.0, to use OrderedDict for
situations that require insertion-ordered keys.

אורי

unread,
Apr 11, 2020, 1:40:40 AM4/11/20
to Django developers (Contributions to Django itself)
Hi James,

In this form I need to insert fields in the beginning of the form, and therefore I call move_to_end. It worked with Django 2.2 but not with 3.0 because this method is not defined in a dict. So I think if you revert to using OrderedDict, users will be able to use this method to insert fields in the beginning, or change the order of fields. So I think it was a mistake changing it to a dict.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAL13Cg_H_S5W1FpcC8OuGDh7mDLrPqm4MDg6dmxRtGVit7JE_Q%40mail.gmail.com.

James Bennett

unread,
Apr 11, 2020, 2:10:46 AM4/11/20
to django-d...@googlegroups.com
On Fri, Apr 10, 2020 at 10:40 PM ‫אורי‬‎ <u...@speedy.net> wrote:‬
> In this form I need to insert fields in the beginning of the form, and therefore I call move_to_end. It worked with Django 2.2 but not with 3.0 because this method is not defined in a dict. So I think if you revert to using OrderedDict, users will be able to use this method to insert fields in the beginning, or change the order of fields. So I think it was a mistake changing it to a dict.

Reordering the fields of a form is a use case that already has supported API:

https://docs.djangoproject.com/en/3.0/ref/forms/api/#notes-on-field-ordering

You can either set the field_order attribute explicitly to the order
you want, or call the order_fields() method and pass in the new order.

Manually manipulating the underlying fields dict was never a
documented (and thus never a supported) feature of forms. If you need
to reorder fields, please just use the documented and supported API
for this.

אורי

unread,
Apr 11, 2020, 3:14:43 AM4/11/20
to Django developers (Contributions to Django itself)
Hi James,

Thanks for your feedback. I updated my code and submitted a new answer to my question on Stack Overflow.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages