Code

Opened 5 years ago

Closed 3 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

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

Download all attachments as: .zip

Change History (11)

comment:1 Changed 4 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 3 years ago by mattmcc

  • Severity set to Normal
  • Type set to Bug

comment:3 Changed 3 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 3 years ago by oinopion

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

comment:5 Changed 3 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 3 years ago by oinopion

comment:6 Changed 3 years ago by oinopion

Added documentation.

comment:7 Changed 3 years ago by oinopion

  • Needs documentation unset

comment:8 Changed 3 years ago by oinopion

  • Keywords QueryDict, dceu2011 added; QueryDict removed
  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 3 years ago by hvdklauw

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

comment:10 Changed 3 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.