如何在不调用API调用的情况下从DRF中的ModelViewSet获取数据



我将把所有的API转换为gRPC调用。目前,我能够将所有ViewSet转移到gRPC中(示例代码添加到这个问题的末尾(。但是ModelViewSet会出现这样的错误。

Traceback (most recent call last):
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/venv/lib/python3.6/site-packages/grpc/_server.py", line 435, in _call_behavior
response_or_iterator = behavior(argument, context)
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/servicers/tenant/main.py", line 15, in get_tenant
data = ClientViewSet().list(request=original_request, )
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/common/lib/decorators.py", line 128, in wrapper_format_response
final_data = call_func(func, self, request, transaction, exception, *args, **kwargs)
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/common/lib/decorators.py", line 99, in call_func
return func(self, request, *args, **kwargs)
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/api_v1/viewsets.py", line 471, in list
data = super().list(request, *args, **kwargs).data
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/venv/lib/python3.6/site-packages/rest_framework/mixins.py", line 38, in list
queryset = self.filter_queryset(self.get_queryset())
File "/home/wasdkiller/PycharmProjects/ocsa/dataengine-service/venv/lib/python3.6/site-packages/rest_framework/generics.py", line 158, in filter_queryset
queryset = backend().filter_queryset(self.request, queryset, self)
AttributeError: 'ClientViewSet' object has no attribute 'request'

所以我的viewsets.py看起来是这样的(format_response装饰器将其转换为Response对象(

class ClientViewSet(viewsets.ModelViewSet):
queryset = Client.objects.all()
serializer_class = ser.ClientSerializer
@format_response(exception=False)
def list(self, request, *args, **kwargs):
data = super().list(request, *args, **kwargs).data
# data = {}
return data, True, HTTPStatus.OK, 'data retrieve successfully'

当我将其称为API时,它可以完美地工作。但是我想在不调用API的情况下做同样的事情。以下是我解决问题的方法,

from django.http import HttpRequest
from rest_framework.request import Request

# creating request object
django_request = HttpRequest()
django_request.method = 'GET'
drf_request = Request(django_request)
data = ClientViewSet().list(request=drf_request)
print(f'data: {data.data}')

ClientViewSet中的super()函数有问题,但如果我取消对data = {}的注释并注释掉调用的super()函数,则它既适用于API,也适用于上述方法。我在发生错误的DRF代码库中进行了检查,当调用API时,self对象有request对象,而上面的方法不存在。

简而言之,您需要在视图集上调用as_view()并映射http谓词,然后使用适当的HttpRequest调用该函数。

django中的所有视图最终都是函数,DRF是最重要的。在路由完成后,"ViewSet"就不以正常的方式作为独立类存在了。

django_request = HttpRequest()
django_request.method = 'GET'
my_view = ClientViewSet.as_view({'get': 'list', 'post':'create'})
data = my_view(request=django_request)
print(f'data: {data.data}')

如果需要详细路线(users/37,…(,则需要创建一个映射到这些视图集函数的新视图函数。这不可能是同一个视图函数,因为httpget现在需要在视图集中指向与列表情况不同的函数。请参阅routers.py源代码,了解正在发生的事情以及映射到哪里的内容。

# map http 'get' to the 'retrive' function on the viewset
my_view = ClientViewSet.as_view({'get': 'retrieve', ...})
# pass in the kwarg the URL routing would normally extract
client_pk = 9329032 
data = my_view(request=django_request, pk=client_pk)

如果你想看看你的视图集的所有映射是什么,那么你可以使用以下代码段打印出来:

router = SimpleRouter()
router.register("client", ClientViewSet, basename="client")
for url in router.urls:  # type: URLPattern
print(f"{url.pattern} ==> {url.callback}")
for verb, action in url.callback.actions.items():
print(f"    {verb} -> {action}")
# output will be something like this
^client/$ ==> <function ClientViewSet at 0x11b91c280>
get -> list
^client/(?P<pk>[^/.]+)/$ ==> <function ClientViewSet at 0x11b91c3a0>
get -> retrieve
put -> update
patch -> partial_update
delete -> destroy

传递给该视图的kwargs将取决于ViewSet中类似lookup_url_kwarg的设置,但在大多数情况下,它们都很简单。

相关内容

  • 没有找到相关文章

最新更新