Opened 7 years ago

Closed 5 years ago

#12375 closed Bug (fixed)

updating QueryDict in another dict gives unexpected results.

Reported by: hvdklauw Owned by: oinopion
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


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

Download all attachments as: .zip

Change History (11)

comment:1 Changed 7 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 mattmcc

  • Severity set to Normal
  • Type set to Bug

comment:3 Changed 5 years ago by oinopion

  • Easy pickings unset
  • Owner changed from nobody to oinopion
  • Status changed from new to assigned
  • UI/UX unset

comment:4 Changed 5 years ago by oinopion

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 .

Changed 5 years ago by oinopion

comment:6 Changed 5 years ago by oinopion

Added documentation.

comment:7 Changed 5 years ago by oinopion

  • Needs documentation unset

comment:8 Changed 5 years ago by oinopion

  • Keywords dceu2011 added
  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 5 years ago by hvdklauw

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

comment:10 Changed 5 years ago by jezdez

  • Resolution set to fixed
  • Status changed from assigned to closed

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