Django Views: When is request.data a dict vs a QueryDict?











up vote
0
down vote

favorite












I have run into some trouble with the issue, that request.data sometimes is a dict (especially when testing) and sometimes a QueryDict instance (when using curl).



This is especially a problem because apparently there is a big difference when calling a view using curl like so:



curl -X POST --data "some_float=1.23456789012123123" "http://localhost:8000/myview"


Or using the django_webtest client like so:



class APIViewTest(WebTest):
def test_testsomething(self):
self.app.post(url=url, params=json.dumps({some_float=1.26356756467}))


And then casting that QueryDict to a dict like so



new_dict = dict(**request.data)
my_float = float(new_dict['some_float'])


Everything works fine in the tests, as there request.data is a dict, but in production the view crashes because new_dict['some_float'] is actually a list with one element, and not as expected a float.



I have considered fixing the issue like so:



    if type(request.data) is dict:
new_dict = dict(**request.data)
else:
new_dict = dict(**request.data.dict())


which feels very wrong as the tests would only test line 2, and (some? all?) production code would run line 4.



So while I am wondering why QueryDict behaves in this way, I would rather know why and when response.data is a QueryDict in the first place. And how I can use django tests to simulate this behavior. Having different conditions for production and testing systems is always troublesome and sometimes unavoidable, but in this case I feel like it could be fixed. Or is this a specific issue related to django_webtest?










share|improve this question




















  • 1




    Because you can pass two values for the same key, like: curl -X POST --data "some_float=1.23456789012123123" --data "some_float=3.14" "http://localhost:8000/myview". This in essence why they built a QueryDict in the first place.
    – Willem Van Onsem
    Nov 8 at 10:58










  • But shouldn't request.data then always be a QueryDict instance?
    – David Nathan
    Nov 8 at 11:02










  • It is, it is only because you here convert it to adict, that it is, of course, a dictionary.
    – Willem Van Onsem
    Nov 8 at 11:03










  • No it is not. type(request.data) is dict returns True when testing locally as explained in my question.
    – David Nathan
    Nov 8 at 11:06















up vote
0
down vote

favorite












I have run into some trouble with the issue, that request.data sometimes is a dict (especially when testing) and sometimes a QueryDict instance (when using curl).



This is especially a problem because apparently there is a big difference when calling a view using curl like so:



curl -X POST --data "some_float=1.23456789012123123" "http://localhost:8000/myview"


Or using the django_webtest client like so:



class APIViewTest(WebTest):
def test_testsomething(self):
self.app.post(url=url, params=json.dumps({some_float=1.26356756467}))


And then casting that QueryDict to a dict like so



new_dict = dict(**request.data)
my_float = float(new_dict['some_float'])


Everything works fine in the tests, as there request.data is a dict, but in production the view crashes because new_dict['some_float'] is actually a list with one element, and not as expected a float.



I have considered fixing the issue like so:



    if type(request.data) is dict:
new_dict = dict(**request.data)
else:
new_dict = dict(**request.data.dict())


which feels very wrong as the tests would only test line 2, and (some? all?) production code would run line 4.



So while I am wondering why QueryDict behaves in this way, I would rather know why and when response.data is a QueryDict in the first place. And how I can use django tests to simulate this behavior. Having different conditions for production and testing systems is always troublesome and sometimes unavoidable, but in this case I feel like it could be fixed. Or is this a specific issue related to django_webtest?










share|improve this question




















  • 1




    Because you can pass two values for the same key, like: curl -X POST --data "some_float=1.23456789012123123" --data "some_float=3.14" "http://localhost:8000/myview". This in essence why they built a QueryDict in the first place.
    – Willem Van Onsem
    Nov 8 at 10:58










  • But shouldn't request.data then always be a QueryDict instance?
    – David Nathan
    Nov 8 at 11:02










  • It is, it is only because you here convert it to adict, that it is, of course, a dictionary.
    – Willem Van Onsem
    Nov 8 at 11:03










  • No it is not. type(request.data) is dict returns True when testing locally as explained in my question.
    – David Nathan
    Nov 8 at 11:06













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have run into some trouble with the issue, that request.data sometimes is a dict (especially when testing) and sometimes a QueryDict instance (when using curl).



This is especially a problem because apparently there is a big difference when calling a view using curl like so:



curl -X POST --data "some_float=1.23456789012123123" "http://localhost:8000/myview"


Or using the django_webtest client like so:



class APIViewTest(WebTest):
def test_testsomething(self):
self.app.post(url=url, params=json.dumps({some_float=1.26356756467}))


And then casting that QueryDict to a dict like so



new_dict = dict(**request.data)
my_float = float(new_dict['some_float'])


Everything works fine in the tests, as there request.data is a dict, but in production the view crashes because new_dict['some_float'] is actually a list with one element, and not as expected a float.



I have considered fixing the issue like so:



    if type(request.data) is dict:
new_dict = dict(**request.data)
else:
new_dict = dict(**request.data.dict())


which feels very wrong as the tests would only test line 2, and (some? all?) production code would run line 4.



So while I am wondering why QueryDict behaves in this way, I would rather know why and when response.data is a QueryDict in the first place. And how I can use django tests to simulate this behavior. Having different conditions for production and testing systems is always troublesome and sometimes unavoidable, but in this case I feel like it could be fixed. Or is this a specific issue related to django_webtest?










share|improve this question















I have run into some trouble with the issue, that request.data sometimes is a dict (especially when testing) and sometimes a QueryDict instance (when using curl).



This is especially a problem because apparently there is a big difference when calling a view using curl like so:



curl -X POST --data "some_float=1.23456789012123123" "http://localhost:8000/myview"


Or using the django_webtest client like so:



class APIViewTest(WebTest):
def test_testsomething(self):
self.app.post(url=url, params=json.dumps({some_float=1.26356756467}))


And then casting that QueryDict to a dict like so



new_dict = dict(**request.data)
my_float = float(new_dict['some_float'])


Everything works fine in the tests, as there request.data is a dict, but in production the view crashes because new_dict['some_float'] is actually a list with one element, and not as expected a float.



I have considered fixing the issue like so:



    if type(request.data) is dict:
new_dict = dict(**request.data)
else:
new_dict = dict(**request.data.dict())


which feels very wrong as the tests would only test line 2, and (some? all?) production code would run line 4.



So while I am wondering why QueryDict behaves in this way, I would rather know why and when response.data is a QueryDict in the first place. And how I can use django tests to simulate this behavior. Having different conditions for production and testing systems is always troublesome and sometimes unavoidable, but in this case I feel like it could be fixed. Or is this a specific issue related to django_webtest?







python django django-rest-framework django-webtest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 11:21









Daniel Roseman

438k40568624




438k40568624










asked Nov 8 at 10:55









David Nathan

2,44822142




2,44822142








  • 1




    Because you can pass two values for the same key, like: curl -X POST --data "some_float=1.23456789012123123" --data "some_float=3.14" "http://localhost:8000/myview". This in essence why they built a QueryDict in the first place.
    – Willem Van Onsem
    Nov 8 at 10:58










  • But shouldn't request.data then always be a QueryDict instance?
    – David Nathan
    Nov 8 at 11:02










  • It is, it is only because you here convert it to adict, that it is, of course, a dictionary.
    – Willem Van Onsem
    Nov 8 at 11:03










  • No it is not. type(request.data) is dict returns True when testing locally as explained in my question.
    – David Nathan
    Nov 8 at 11:06














  • 1




    Because you can pass two values for the same key, like: curl -X POST --data "some_float=1.23456789012123123" --data "some_float=3.14" "http://localhost:8000/myview". This in essence why they built a QueryDict in the first place.
    – Willem Van Onsem
    Nov 8 at 10:58










  • But shouldn't request.data then always be a QueryDict instance?
    – David Nathan
    Nov 8 at 11:02










  • It is, it is only because you here convert it to adict, that it is, of course, a dictionary.
    – Willem Van Onsem
    Nov 8 at 11:03










  • No it is not. type(request.data) is dict returns True when testing locally as explained in my question.
    – David Nathan
    Nov 8 at 11:06








1




1




Because you can pass two values for the same key, like: curl -X POST --data "some_float=1.23456789012123123" --data "some_float=3.14" "http://localhost:8000/myview". This in essence why they built a QueryDict in the first place.
– Willem Van Onsem
Nov 8 at 10:58




Because you can pass two values for the same key, like: curl -X POST --data "some_float=1.23456789012123123" --data "some_float=3.14" "http://localhost:8000/myview". This in essence why they built a QueryDict in the first place.
– Willem Van Onsem
Nov 8 at 10:58












But shouldn't request.data then always be a QueryDict instance?
– David Nathan
Nov 8 at 11:02




But shouldn't request.data then always be a QueryDict instance?
– David Nathan
Nov 8 at 11:02












It is, it is only because you here convert it to adict, that it is, of course, a dictionary.
– Willem Van Onsem
Nov 8 at 11:03




It is, it is only because you here convert it to adict, that it is, of course, a dictionary.
– Willem Van Onsem
Nov 8 at 11:03












No it is not. type(request.data) is dict returns True when testing locally as explained in my question.
– David Nathan
Nov 8 at 11:06




No it is not. type(request.data) is dict returns True when testing locally as explained in my question.
– David Nathan
Nov 8 at 11:06












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Your test isn't a reflection of your actual curl call.



In your test, you post JSON, which is then available as a dict from request.data. But your curl call posts standard form data, which is available as a QueryDict. This behaviour is managed by the parsers attribute of your view or the DEFAULT_PARSER_CLASSES settings - and further note that this is functionality specifically provided by django-rest-framework, which you should have tagged in your question, not Django itself.



Really you should test the same thing as you are doing; either send JSON from curl or get your test to post form-data.






share|improve this answer























  • Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
    – David Nathan
    Nov 8 at 11:25












  • But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
    – Daniel Roseman
    Nov 8 at 11:34












  • And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
    – Daniel Roseman
    Nov 8 at 11:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206284%2fdjango-views-when-is-request-data-a-dict-vs-a-querydict%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Your test isn't a reflection of your actual curl call.



In your test, you post JSON, which is then available as a dict from request.data. But your curl call posts standard form data, which is available as a QueryDict. This behaviour is managed by the parsers attribute of your view or the DEFAULT_PARSER_CLASSES settings - and further note that this is functionality specifically provided by django-rest-framework, which you should have tagged in your question, not Django itself.



Really you should test the same thing as you are doing; either send JSON from curl or get your test to post form-data.






share|improve this answer























  • Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
    – David Nathan
    Nov 8 at 11:25












  • But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
    – Daniel Roseman
    Nov 8 at 11:34












  • And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
    – Daniel Roseman
    Nov 8 at 11:39















up vote
2
down vote



accepted










Your test isn't a reflection of your actual curl call.



In your test, you post JSON, which is then available as a dict from request.data. But your curl call posts standard form data, which is available as a QueryDict. This behaviour is managed by the parsers attribute of your view or the DEFAULT_PARSER_CLASSES settings - and further note that this is functionality specifically provided by django-rest-framework, which you should have tagged in your question, not Django itself.



Really you should test the same thing as you are doing; either send JSON from curl or get your test to post form-data.






share|improve this answer























  • Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
    – David Nathan
    Nov 8 at 11:25












  • But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
    – Daniel Roseman
    Nov 8 at 11:34












  • And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
    – Daniel Roseman
    Nov 8 at 11:39













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Your test isn't a reflection of your actual curl call.



In your test, you post JSON, which is then available as a dict from request.data. But your curl call posts standard form data, which is available as a QueryDict. This behaviour is managed by the parsers attribute of your view or the DEFAULT_PARSER_CLASSES settings - and further note that this is functionality specifically provided by django-rest-framework, which you should have tagged in your question, not Django itself.



Really you should test the same thing as you are doing; either send JSON from curl or get your test to post form-data.






share|improve this answer














Your test isn't a reflection of your actual curl call.



In your test, you post JSON, which is then available as a dict from request.data. But your curl call posts standard form data, which is available as a QueryDict. This behaviour is managed by the parsers attribute of your view or the DEFAULT_PARSER_CLASSES settings - and further note that this is functionality specifically provided by django-rest-framework, which you should have tagged in your question, not Django itself.



Really you should test the same thing as you are doing; either send JSON from curl or get your test to post form-data.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 11:24

























answered Nov 8 at 11:17









Daniel Roseman

438k40568624




438k40568624












  • Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
    – David Nathan
    Nov 8 at 11:25












  • But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
    – Daniel Roseman
    Nov 8 at 11:34












  • And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
    – Daniel Roseman
    Nov 8 at 11:39


















  • Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
    – David Nathan
    Nov 8 at 11:25












  • But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
    – Daniel Roseman
    Nov 8 at 11:34












  • And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
    – Daniel Roseman
    Nov 8 at 11:39
















Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
– David Nathan
Nov 8 at 11:25






Unfortunately my clients send both. I hoped django had some sort of interface which could deal with both in the same way
– David Nathan
Nov 8 at 11:25














But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
– Daniel Roseman
Nov 8 at 11:34






But as I just said, that is DRF functionality which is controlled by the parsers setting, you are free to override this or set your own. In any case, the question "when is request.data a dict vs a querydict" is answered by "when you send JSON vs form-data".
– Daniel Roseman
Nov 8 at 11:34














And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
– Daniel Roseman
Nov 8 at 11:39




And additionally, request.data is the interface that deals with both in the same way. I don't know why you think you need to convert to a dict at all, a QueryDict operates exactly like a dict in all respects other than it can additionally handle multiple values for the same key. If you don't have multiple values, just use it like a dict and you won't notice any difference.
– Daniel Roseman
Nov 8 at 11:39


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206284%2fdjango-views-when-is-request-data-a-dict-vs-a-querydict%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)