【发布时间】:2015-02-09 23:59:22
【问题描述】:
假设我有这个 APIView
class Dummy(APIView):
def get(self, request):
return Response(data=request.query_params.get('uuid'))
为了测试它,我需要创建一个请求对象以传递给get 函数
def test_dummy(self):
from rest_framework.test import APIRequestFactory
factory = APIRequestFactory()
request = factory.get('/?uuid=abcd')
DummyView().get(request)
它抱怨AttributeError: 'WSGIRequest' object has no attribute 'query_params'
仔细观察,工厂创建了一个 WSGIRequest 实例而不是 DRF 版本 <class 'rest_framework.request.Request'>。
>>> from rest_framework.test import APIRequestFactory
>>> factory = APIRequestFactory()
>>> request = factory.get('/')
>>> request.__class__
<class 'django.core.handlers.wsgi.WSGIRequest'>
【问题讨论】:
标签: django django-rest-framework