Post data Query Dict - Why not a list ?

1,380 views
Skip to first unread message

Szabo, Patrick (LNG-VIE)

unread,
Feb 29, 2012, 3:46:14 AM2/29/12
to django...@googlegroups.com

Hi,

 

If i do

 

for key in request.POST.keys():

print request.POST[key]

 

Over this (which is the result of print request.Post):

 

<QueryDict: {u'hix': [u'Monatsreport'], u'my_choice_field': [u'2', u'29', u'42']}>

 

I get:

 

Monatsreport

42

 

Should I not get  lists ?

 

i.e:

 

[Monatsreport]

[2,29,42]

 

I need those other information from  'my_choice_field' how do I get them ?

 

Help would be much appreciated.

 

Best regards

 

. . . . . . . . . . . . . . . . . . . . . . . . . .

Ing. Patrick Szabo
XSLT Developer

LexisNexis
A-1030 Wien, Marxergasse 25

patric...@lexisnexis.at

Tel.: +43 1 53452 1573

Fax: +43 1 534 52 146



Masklinn

unread,
Feb 29, 2012, 4:03:23 AM2/29/12
to django...@googlegroups.com
On 2012-02-29, at 09:46 , Szabo, Patrick (LNG-VIE) wrote:
> Hi,
>
> If i do
>
> for key in request.POST.keys():
>
> print request.POST[key]
>
> Over this (which is the result of print request.Post):
>
> <QueryDict: {u'hix': [u'Monatsreport'], u'my_choice_field': [u'2',
> u'29', u'42']}>
>
> I get:
>
> Monatsreport
>
> 42
>
> Should I not get lists ?

No, multidicts generally conform to the dict interface (mapping a
key to a value) as it's the most useful way to interact with POSTDATA
(or GETDATA for that matter): people will generally map a single value
to each key and having to unpack that value all the time is a pain.

If you want to get all values for the key, as the documentation
indicates[0] you should use the `getlist` method.

[0] https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict.getlist

Szabo, Patrick (LNG-VIE)

unread,
Feb 29, 2012, 4:08:55 AM2/29/12
to django...@googlegroups.com
Exactly what i was looking for...thx a lot!


. . . . . . . . . . . . . . . . . . . . . . . . . .
Ing. Patrick Szabo
XSLT Developer
LexisNexis
A-1030 Wien, Marxergasse 25

mailto:patric...@lexisnexis.at


-----Ursprüngliche Nachricht-----

[0] https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict.getlist

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Martin J. Laubach

unread,
Feb 29, 2012, 4:13:25 AM2/29/12
to django...@googlegroups.com
Also, having a form to parse and validate the POST data is really the way to go. Even if you never actually show the form to the user, for example in an AJAX callback, deferring all that responsibility and just use "form.is_valid()" and "form.cleaned_data" is a lot better than doing parameter validation in the view.

mjl

Andre Terra

unread,
Feb 29, 2012, 8:46:36 AM2/29/12
to django...@googlegroups.com
On Wed, Feb 29, 2012 at 5:46 AM, Szabo, Patrick (LNG-VIE) <patric...@lexisnexis.at> wrote:

Should I not get  lists ?

 

i.e:

 

[Monatsreport]

[2,29,42]



I may be misunderstanding something, but for one reason lists are not ordered, so you'd never know which item to get, agreed?


Cheers,
AT

Tom Evans

unread,
Feb 29, 2012, 9:32:38 AM2/29/12
to django...@googlegroups.com
On Wed, Feb 29, 2012 at 1:46 PM, Andre Terra <andre...@gmail.com> wrote:
> I may be misunderstanding something, but for one reason lists are not
> ordered, so you'd never know which item to get, agreed?
>

Lists are ordered, I think you are confusing something :)

Cheers

Tom

Andre Terra

unread,
Feb 29, 2012, 9:57:28 AM2/29/12
to django...@googlegroups.com
Excuse my brainfart moment.. sets and dicts are not ordered!

Reply all
Reply to author
Forward
0 new messages