The behavior of QueryDict.get(_key_, _default=None_)

616 views
Skip to first unread message

Philip Lee

unread,
Jul 19, 2018, 2:19:34 AM7/19/18
to Django developers (Contributions to Django itself)
I was trapped by the behavior of `QueryDict.get(_key_, _default=None_)` for a bit while before consulting the documentation , for that *If the key has more than one value, it returns the last value.*
So as for  multiple values for the same key,  is returning the last value of key more often expected than returning all the values of the key(that is the behavior of `QueryDict.getlist(_key_, _default=None_)` right now)?  If not, I'd suggest to change the behavior of `QueryDict.get(_key_, _default=None_)` to return all the values of the same key for a natural usage so that avoid confusing problems caused by the current behavior like 

ludovic coues

unread,
Jul 19, 2018, 2:50:09 AM7/19/18
to django-d...@googlegroups.com
This will probably breaking compatibility with previous version of Django, breaking a lot of website in subtle way.

Printing a debug message in the log when calling QueryDict.get on key being present more than once sound more reasonable. That should be doable as an third party app but that wouldn't help beginners.

--
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 post to this group, send email to django-d...@googlegroups.com.
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/5a6263e2-5872-400c-af1b-aa2c74244247%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carlton Gibson

unread,
Jul 19, 2018, 2:52:56 AM7/19/18
to Django developers (Contributions to Django itself)
Hi Philip. 

This is unlikely(!!!) to be changed. As it says on the ticket, it's considered a feature, not a bug. 

The usual case is to need a single value from the query string, so `[]` and `get()` both return scalars. 
`getList()` exists specifically for the case where you do want multiple values. 

Having said all that, I think a lot of people new to Django get up and running, and well beyond that, 
without ever knowing that `getList()` exists. (I guess they discover it, with Google/StackOverflow, first 
time they need it.) 

A quick scan of the tutorial and docs suggests that `getList()` is probably under-documented. (It appears just twice in `request-response.txt`.) 
Maybe an addition to the beginning of the QueryDict section demonstrating the basic use-cases would be a good addition.

Kind Regards,

Carlton

Philip Lee

unread,
Jul 19, 2018, 7:12:07 AM7/19/18
to Django developers (Contributions to Django itself)
so maybe we should add to the documentation of 
If want to get multiple values for the same key, use QueryDict.getlist(key, default=None) instead of QueryDict.get(key, default=None)

add example to 
>>> q = QueryDict('a=1&a=2&c=3')
>>> q.get('a')
'3'
>>> q.getlist('a')
['1', '2', '3']


Florian Apolloner

unread,
Jul 19, 2018, 8:44:41 AM7/19/18
to Django developers (Contributions to Django itself)
On Thursday, July 19, 2018 at 8:52:56 AM UTC+2, Carlton Gibson wrote:
The usual case is to need a single value from the query string, so `[]` and `get()` both return scalars. 
`getList()` exists specifically for the case where you do want multiple values. 

I'd like to expand on this, because the current behaviour has even more important aspects. The query string is supplied by the user and as such is completely untrusted data. If `.get` were to change to return lists if there are lists, this would mean that the user could control the data structures in the view. This is not something we'd ever want.

Aside from that it is technically impossible to change the behaviour of get to return lists: What would ?some_param=1 result in? Would it all of a sudden be a list with one item? Since there is no difference between a scalar value and a one item list in terms of query strings, this has to stay the decision of the developer. They are the only ones knowing how the data should look like and can handle it accordingly.

Cheers,
Florian

Zhao Lee

unread,
Jul 19, 2018, 10:27:37 PM7/19/18
to django-developers

I have to confess I don’t know much about web development .

I used to expect QueryDict.get(_key_, _default=None_) to return data type that client side offered, for example , if client side send {'wordPos':(2,3)} , I expect get('wordPos') to return it, whereas the current behavior is far from explanation , even using getlist('wordPos'), it just returns ['2','3'], then I have to do additional work to convert it back to its first shape, just found this inconvenient, so I made this post.
BTW, why integers were converted to strings after the transmission?




--
You received this message because you are subscribed to a topic in the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/snBepnO5AjY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.

To post to this group, send email to django-d...@googlegroups.com.
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/4e15b2a8-895e-4c43-9b61-5dce2759d23c%40googlegroups.com.

Collin Anderson

unread,
Jul 19, 2018, 10:55:11 PM7/19/18
to django-d...@googlegroups.com
Using your example, the client is sending this string:

?wordPos=2&wordPos=3

It's not an array like ?wordPos=2,3

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 post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.

Collin Anderson

unread,
Jul 19, 2018, 10:58:05 PM7/19/18
to django-d...@googlegroups.com
You might want to try json encoding your data if you want to preserve integers, lists, etc.

Zhao Lee

unread,
Jul 20, 2018, 10:36:55 PM7/20/18
to django-developers
Yes, that's how I got confused by the behavior of get('wordPos') at first , it just returns '3', not (2,3)


To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAFO84S7i7YOrqmWamGo5vwr8RW0L6EBuvdsBEAh-0LjDwNtNEA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages