QueryDict and ordering

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

QueryDict and ordering

Ole Laursen-3
Hi!

Would it be possible to derive QueryDict (i.e. MultiValueDict) from an OrderedDict instead of dict?

I'm finding it increasingly irritating that the original order is kept by the whole stack right until Django puts it into a dict. It makes some highly dynamic form situations more tedious to handle.

Now that Python 3.6 preserves the order of dicts as an implementation detail, there should not be any performance overhead as far as I'm aware.


Ole

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/5b49e5c8-68df-41c8-96b8-1ba6f36fa0cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: QueryDict and ordering

Tim Graham-2
Hi, Did you try writing a patch? I naively tried "class MultiValueDict(OrderedDict):" as the only change and it doesn't pass the tests. Perhaps more adaptations are required.

As for the motivation, I'm not sure if you described the problem in enough detail. (i.e. what does "It makes highly dynamic form situations more tedious to handle" mean?)

If Python 3.6 preserves order, is this only an issue on Python 3.5? If so, I'd guess there would be little motivation in making a change (if it's a non-trivial change) given 3.5 will be end-of-life soon enough (2020).

On Friday, January 5, 2018 at 10:07:41 AM UTC-5, Ole Laursen wrote:
Hi!

Would it be possible to derive QueryDict (i.e. MultiValueDict) from an OrderedDict instead of dict?

I'm finding it increasingly irritating that the original order is kept by the whole stack right until Django puts it into a dict. It makes some highly dynamic form situations more tedious to handle.

Now that Python 3.6 preserves the order of dicts as an implementation detail, there should not be any performance overhead as far as I'm aware.


Ole

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/493aede0-5a74-4a22-aebf-c00a29f02cf5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: QueryDict and ordering

Ole Laursen-3
2018-01-05 17:12 GMT+01:00 Tim Graham <[hidden email]>:
> Hi, Did you try writing a patch? I naively tried "class
> MultiValueDict(OrderedDict):" as the only change and it doesn't pass the
> tests. Perhaps more adaptations are required.

Tried just now, and yeah it takes a little more adaptation.
MultiValueDict is relying on __setitem__ not being called by the
underlying dict implementation in a couple of places, e.g.
MultiValueDict({ "foo": [ "bar"] }). I'm happy to write a little patch
if the idea makes sense.


> As for the motivation, I'm not sure if you described the problem in enough
> detail. (i.e. what does "It makes highly dynamic form situations more
> tedious to handle" mean?)

If you are using Javascript to insert extra forms or rearrange input
fields, and the order matters, then you can't use request.POST without
some kind of bookkeeping workaround. Either by encoding and
maintaining a sequence number client-side, or by adding extra fields
to handle it.


For instance, I have a bunch of rows like this where rows can be
rearranged by dragging:

  Foo: 1___       Bar:  hello______       [x]

  Foo: 3___       Baz:  [ Choice 1 ]        [x]

                                         [Add row]

I'm using a form to validate each row - so each have a name prefix.
Ideally, I could just go through request.POST.keys(), look for each
new set of values and instantiate forms as needed.


> If Python 3.6 preserves order, is this only an issue on Python 3.5? If so,
> I'd guess there would be little motivation in making a change (if it's a
> non-trivial change) given 3.5 will be end-of-life soon enough (2020).

Unfortunately, it's only preserved as an implementation detail, if you
write code that depends on the order being preserved, then you need to
derive from OrderedDict. There was a thread on the Python list, and
you can see here

https://docs.python.org/3.6/whatsnew/3.6.html

that they're careful not to say that dict preserves the order.


Ole

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CANb2OvL8KxkD3ug_C0RDK0hnyQWup09OE%2BVx0%3DLCSfRcW%2B_BSw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: QueryDict and ordering

Tim Graham-2
Preservation of dict ordering is guaranteed in Python 3.7+ so that officially fixes this, correct?
https://mail.python.org/pipermail/python-dev/2017-December/151283.html

On Friday, January 5, 2018 at 1:14:03 PM UTC-5, Ole Laursen wrote:
2018-01-05 17:12 GMT+01:00 Tim Graham <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="9AaV6zeyDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">timog...@...>:
> Hi, Did you try writing a patch? I naively tried "class
> MultiValueDict(OrderedDict):" as the only change and it doesn't pass the
> tests. Perhaps more adaptations are required.

Tried just now, and yeah it takes a little more adaptation.
MultiValueDict is relying on __setitem__ not being called by the
underlying dict implementation in a couple of places, e.g.
MultiValueDict({ "foo": [ "bar"] }). I'm happy to write a little patch
if the idea makes sense.


> As for the motivation, I'm not sure if you described the problem in enough
> detail. (i.e. what does "It makes highly dynamic form situations more
> tedious to handle" mean?)

If you are using Javascript to insert extra forms or rearrange input
fields, and the order matters, then you can't use request.POST without
some kind of bookkeeping workaround. Either by encoding and
maintaining a sequence number client-side, or by adding extra fields
to handle it.


For instance, I have a bunch of rows like this where rows can be
rearranged by dragging:

  Foo: 1___       Bar:  hello______       [x]

  Foo: 3___       Baz:  [ Choice 1 ]        [x]

                                         [Add row]

I'm using a form to validate each row - so each have a name prefix.
Ideally, I could just go through request.POST.keys(), look for each
new set of values and instantiate forms as needed.


> If Python 3.6 preserves order, is this only an issue on Python 3.5? If so,
> I'd guess there would be little motivation in making a change (if it's a
> non-trivial change) given 3.5 will be end-of-life soon enough (2020).

Unfortunately, it's only preserved as an implementation detail, if you
write code that depends on the order being preserved, then you need to
derive from OrderedDict. There was a thread on the Python list, and
you can see here

<a href="https://docs.python.org/3.6/whatsnew/3.6.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.python.org%2F3.6%2Fwhatsnew%2F3.6.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFuhvAbcix-OAg71yMIULEyhZAhww&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.python.org%2F3.6%2Fwhatsnew%2F3.6.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFuhvAbcix-OAg71yMIULEyhZAhww&#39;;return true;">https://docs.python.org/3.6/whatsnew/3.6.html

that they're careful not to say that dict preserves the order.


Ole

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/e390b4dd-d33e-49a1-bc6c-94987c13f460%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: QueryDict and ordering

Todor Velichkov
In reply to this post by Ole Laursen-3
Is this what you are looking for?


from django.http import QueryDict
from collections import OrderedDict

class OrderedQueryDict(QueryDict, OrderedDict):
   
pass

my_dict
= OrderedQueryDict(request.META['QUERY_STRING'])

print my_dict.items()



On Friday, January 5, 2018 at 5:07:41 PM UTC+2, Ole Laursen wrote:
Hi!

Would it be possible to derive QueryDict (i.e. MultiValueDict) from an OrderedDict instead of dict?

I'm finding it increasingly irritating that the original order is kept by the whole stack right until Django puts it into a dict. It makes some highly dynamic form situations more tedious to handle.

Now that Python 3.6 preserves the order of dicts as an implementation detail, there should not be any performance overhead as far as I'm aware.


Ole

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1913a9a7-e2e2-4373-8486-7ffd9bbf53fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: QueryDict and ordering

Ole Laursen-3
In reply to this post by Tim Graham-2
2018-01-05 19:24 GMT+01:00 Tim Graham <[hidden email]>:
> Preservation of dict ordering is guaranteed in Python 3.7+ so that
> officially fixes this, correct?
> https://mail.python.org/pipermail/python-dev/2017-December/151283.html

Sure, I can wait for that. Sorry for the noise, I should read that
list more often.


Ole

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CANb2Ov%2BRJHiDMjYXK64wXxmom0KCEtqRARwJkwGp3Xko60%3DcPg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.