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:
Ian Stapleton Cordasco 2018-01-04 14:31:30 -06:00
parent 522d453546
commit a8fb8ed6ac
2 changed files with 11 additions and 2 deletions

View File

@ -153,6 +153,12 @@ class Request(object):
def __init__(self, request, parsers=None, authenticators=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), (
'The `request` argument must be an instance of '
'`django.http.HttpRequest`, not `{}.{}`.'

View File

@ -33,10 +33,13 @@ class TestInitializer(TestCase):
message = (
'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):
Request(request)
Request(None)
other_request = Request(request)
assert other_request._request is request._request
class PlainTextParser(BaseParser):