Django Rest Framework APIRequestFactory request object has no attribute 'query_params'
Refer to Tom's solution, DummyView()(request)
will raise error:
TypeError: 'DummyView' object is not callable
Instead, should use as_view
just like what you do in urls.py
:
DummyView.as_view()(request)
DRF's as_view
uses method initialize_request
to convert Django Request object to DRF version. You can try with:
from rest_framework.views import APIView
APIView().initialize_request(request)
>>> <rest_framework.request.Request object at 0xad9850c>
You can also use APIClient to run test. It also tests URL dispatching.
from rest_framework.test import APIClient
client = APIClient()
client.post('/notes/', {'title': 'new idea'}, format='json')
That's right. At the moment APIRequestFactory
returns an HttpRequest
object, which only get upgraded to a REST framework Request
object once it gets to the view layer.
This mirrors the behavior that you'll see in an actual request, and what it does do is deal with eg. rendering JSON, XML or whatever other content type you have configured for your test requests.
However I agree that it's surprising behavior and at some point it will probably return a Request
object, and the REST framework view will ensure that it only performs the Request
upgrade on requests that instances of HttpRequest
.
What you need to do in your case is actually call the view, rather than invoking the .get()
method...
factory = APIRequestFactory()
request = factory.get('/?uuid=abcd')
view = DummyView.as_view()
response = view(request) # Calling the view, not calling `.get()`