Opened 14 years ago

Closed 13 years ago

#12375 closed Bug (fixed)

updating QueryDict in another dict gives unexpected results.

Reported by: Harro Owned by: Tomek Paczkowski
Component: HTTP handling Version: 1.1
Severity: Normal Keywords: QueryDict, dceu2011
Cc: hvdklauw@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I noticed a small "bug" in the QueryDict.

When I have an existing dict and want to add the get or postdata to it using my_dict.update(request.POST).
This results in a dict with all the values as lists. I can understand why the QueryDict might work that way, the same key could have multiple values. Except that the QueryDict
does some magic that when you iterate over it it returns the first item in the list.

This limitation should be documented, and maybe a function should be added to the QueryDict to turn it into a normal dict.

Attachments (1)

12375-mvd-dict.diff (2.2 KB ) - added by Tomek Paczkowski 13 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by Russell Keith-Magee, 14 years ago

Triage Stage: UnreviewedAccepted

This is an interesting discrepancy.

I'm inclined to agree with your analysis - my_dict.update(request.POST) should only update values - but it's out of our hands. The python internals dictate the behavior in this case.

Your workaround seems reasonable - i.e., add a dict() method to QueryDict (strictly, to MultiValueDict) that provides a dictionary of key-value pairs, rather than key-list pairs.

comment:2 by Matt McClanahan, 13 years ago

Severity: Normal
Type: Bug

comment:3 by Tomek Paczkowski, 13 years ago

Easy pickings: unset
Owner: changed from nobody to Tomek Paczkowski
Status: newassigned
UI/UX: unset

comment:4 by Tomek Paczkowski, 13 years ago

Added patch with dict() method that returns dictionary with singular values.

comment:5 by rasca, 13 years ago

Has patch: set
Needs documentation: set

Patch is working OK, the test is OK, but there's documentation needed. I believe it should be under https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects .

by Tomek Paczkowski, 13 years ago

Attachment: 12375-mvd-dict.diff added

comment:6 by Tomek Paczkowski, 13 years ago

Added documentation.

comment:7 by Tomek Paczkowski, 13 years ago

Needs documentation: unset

comment:8 by Tomek Paczkowski, 13 years ago

Keywords: dceu2011 added
Triage Stage: AcceptedReady for checkin

comment:9 by Harro, 13 years ago

As the reporter of this I'm happy with the proposed solution

comment:10 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: assignedclosed

In [16418]:

Fixed #12375 -- Added a dict() method to convert a MultiValueDict (such as a QueryDict) to a dictionary of key-value pairs. Thanks, oinopion and hvdklauw.

Note: See TracTickets for help on using tickets.
Back to Top