Opened 7 years ago

Closed 5 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 5 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 7 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
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 Changed 5 years ago by Matt McClanahan

Severity: Normal
Type: Bug

comment:3 Changed 5 years ago by Tomek Paczkowski

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

comment:4 Changed 5 years ago by Tomek Paczkowski

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

comment:5 Changed 5 years ago by rasca

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 .

Changed 5 years ago by Tomek Paczkowski

Attachment: 12375-mvd-dict.diff added

comment:6 Changed 5 years ago by Tomek Paczkowski

Added documentation.

comment:7 Changed 5 years ago by Tomek Paczkowski

Needs documentation: unset

comment:8 Changed 5 years ago by Tomek Paczkowski

Keywords: dceu2011 added
Triage Stage: AcceptedReady for checkin

comment:9 Changed 5 years ago by Harro

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

comment:10 Changed 5 years ago by Jannis Leidel

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