Django Rest Framework APIRequestFactory request object does not have attribute 'query_params'

Suppose I have this APIView

class Dummy(APIView): def get(self, request): return Response(data=request.query_params.get('uuid')) 

To test it, I need to create a request object to go to the get function

 def test_dummy(self): from rest_framework.test import APIRequestFactory factory = APIRequestFactory() request = factory.get('/?uuid=abcd') DummyView().get(request) 

He complains about AttributeError: 'WSGIRequest' object has no attribute 'query_params'

Looking in more detail, the factory creates an instance of WSGIRequest instead of the DRF version <class 'rest_framework.request.Request'> .

 >>> from rest_framework.test import APIRequestFactory >>> factory = APIRequestFactory() >>> request = factory.get('/') >>> request.__class__ <class 'django.core.handlers.wsgi.WSGIRequest'> 
+7
source share
3 answers

It is right. Currently, APIRequestFactory returns an HttpRequest object that only updates to the REST framework Request object when it reaches the presentation level.

This reflects the behavior that you see in a real request, and what it does is, for example, a matter. rendering JSON, XML, or any other type of content that you customized for your test requests.

However, I agree that this is an amazing behavior, and at some point it will probably return a Request object, and presenting the REST structure will ensure that it will perform a Request update on requests for HttpRequest instances.

What you need to do in your case is actually to call the view, and not invoke the .get() method ...

 factory = APIRequestFactory() request = factory.get('/?uuid=abcd') view = DummyView.as_view() response = view(request) # Calling the view, not calling `.get()` 
+11
source

Refer to Tom's solution, DummyView()(request) will DummyView()(request) error:

 TypeError: 'DummyView' object is not callable 

Instead, use as_view just like you do in urls.py :

 DummyView.as_view()(request) 

DRF as_view uses the initialize_request method to convert a Django Request object to a DRF version. You can try:

 from rest_framework.views import APIView APIView().initialize_request(request) >>> <rest_framework.request.Request object at 0xad9850c> 

You can also use APIClient to run the test. It also checks for URL submissions.

 from rest_framework.test import APIClient client = APIClient() client.post('/notes/', {'title': 'new idea'}, format='json') 
+14
source

I understand that this answer is some time after the question was asked, but it solved this problem for me. Just override the APIRequestFactory class as APIRequestFactory below.

 # Override APIRequestFactory to add the query_params attribute. class MyAPIRequestFactory(APIRequestFactory): def generic(self, method, path, data='', content_type='application/octet-stream', secure=False, **extra): # Include the CONTENT_TYPE, regardless of whether or not data is empty. if content_type is not None: extra['CONTENT_TYPE'] = str(content_type) request = super(MyAPIRequestFactory, self).generic( method, path, data, content_type, secure, **extra) request.query_params = request.GET return request 
0
source

Source: https://habr.com/ru/post/982235/


All Articles