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?
python django django-rest-framework django-webtest
add a comment |
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?
python django django-rest-framework django-webtest
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 aQueryDict
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
add a comment |
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?
python django django-rest-framework django-webtest
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
python django django-rest-framework django-webtest
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 aQueryDict
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
add a comment |
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 aQueryDict
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 a
dict
, 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 a
dict
, 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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 aQueryDict
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 a
dict
, 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