mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 12:00:12 +03:00
Allow rest_framework.request.Request instances
Instead of flat-out erroring (which can cause unhandled exceptions) let's perhaps be nicer to users who are considerately and carefully reusing ViewSets in other ViewSets. This means accessing the private attribute for them and then (and only then) raising an AssertionError if it isn't a Django HttpResponse instance. See also #5618
This commit is contained in:
parent
522d453546
commit
a8fb8ed6ac
|
@ -153,6 +153,12 @@ class Request(object):
|
||||||
|
|
||||||
def __init__(self, request, parsers=None, authenticators=None,
|
def __init__(self, request, parsers=None, authenticators=None,
|
||||||
negotiator=None, parser_context=None):
|
negotiator=None, parser_context=None):
|
||||||
|
# If we're being passed our own Request object, unwrap the underlying
|
||||||
|
# HttpRequest. This allows for some backwards compatibilty to 3.7.3
|
||||||
|
# for select users who carefully reuse ViewSets.
|
||||||
|
if isinstance(request, Request):
|
||||||
|
request = request._request
|
||||||
|
|
||||||
assert isinstance(request, HttpRequest), (
|
assert isinstance(request, HttpRequest), (
|
||||||
'The `request` argument must be an instance of '
|
'The `request` argument must be an instance of '
|
||||||
'`django.http.HttpRequest`, not `{}.{}`.'
|
'`django.http.HttpRequest`, not `{}.{}`.'
|
||||||
|
|
|
@ -33,10 +33,13 @@ class TestInitializer(TestCase):
|
||||||
|
|
||||||
message = (
|
message = (
|
||||||
'The `request` argument must be an instance of '
|
'The `request` argument must be an instance of '
|
||||||
'`django.http.HttpRequest`, not `rest_framework.request.Request`.'
|
'`django.http.HttpRequest`, not `NoneType`.'
|
||||||
)
|
)
|
||||||
with self.assertRaisesMessage(AssertionError, message):
|
with self.assertRaisesMessage(AssertionError, message):
|
||||||
Request(request)
|
Request(None)
|
||||||
|
|
||||||
|
other_request = Request(request)
|
||||||
|
assert other_request._request is request._request
|
||||||
|
|
||||||
|
|
||||||
class PlainTextParser(BaseParser):
|
class PlainTextParser(BaseParser):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user