0

Python interpreting QueryDict ambiguously. When I print request.POST values, I see the values I'm expecting i.e 1 and 3. However when I print the list, I see only 3 being printed.

(Pdb) request.POST
<QueryDict: {u'subjob_ids[]': [u' 1 ', u' 3 '], u'updated': [u''], u'created': [u''], u'job_name': [u'Job11'], u'jobtype_id': [u'1'], u'no_of_subjobs': [u'2'], u'operation': [u'addjobwithexistingsubjobs'], u'id': [u'-1']}>

(Pdb) request.POST['subjob_ids[]']
u' 3 '

Checked if this is problem with uunicode, but it's not. When I convert unicode to str , I see only 3.

(Pdb) str(request.POST['subjob_ids[]'])
' 3 '

So is there a problem with the data present in QueryDict?

I tried taking this value into python interpreter instead of pdb, it just works perfectly.

>>> dict = {u'subjob_ids[]': [u' 1 ', u' 3 '], u'updated': [u''], u'created': [u''], u'job_name': [u'Job11'], u'jobtype_id': [u'1'], u'no_of_subjobs': [u'2'], u'operation': [u'addjobwithexistingsubjobs'], u'id': [u'-1']}
>>> dict
{u'subjob_ids[]': [u' 1 ', u' 3 '], u'jobtype_id': [u'1'], u'updated': [u''], u'no_of_subjobs': [u'2'], u'created': [u''], u'operation': [u'addjobwithexistingsubjobs'], u'job_name': [u'Job11'], u'id': [u'-1']}

>>> dict['subjob_ids[]']
[u' 1 ', u' 3 ']

>>> dict['subjob_ids[]'][0]
u' 1 '

>>> dict['subjob_ids[]'][1]
u' 3 '

Is there something wrong with how django is treating the data?

theblackpearl
  • 1,164
  • 3
  • 15
  • 34
  • It's a [MultiValueDict](https://github.com/django/django/blob/master/django/utils/datastructures.py#L48). And no issue, it's just not intended for displaying this way. – spectras Sep 06 '16 at 22:00
  • 4
    Read the docs on [QueryDict](https://docs.djangoproject.com/en/1.10/ref/request-response/#querydict-objects), particularly on the `__getitem__()` and `getlist()` methods. – knbk Sep 06 '16 at 22:02
  • Thanks for pointing me to the source, it did solve the problem :) – theblackpearl Sep 07 '16 at 00:03

0 Answers0