我已经在堆栈溢出和互联网上搜索了一个解决方案,可以从anotehr django restframework APIView调用django restframework ListAPIView。
我试过了:
class ViewA(APIView):
def get(request):
response = ViewB.as_view({'get':'list'})(request)
print response.render()
# do other things with the response
然而,我得到了一个错误:
response = SubsidiariesStatisticsView.as_view({'get': 'list'})(request)
TypeError: as_view() takes exactly 1 argument (2 given)
如何从viewA向viewB传入请求并获得响应?此外,类ViewB
有一个get_serializer_context
方法。如何从ViewA
中调用它
发布于 2019-08-10 17:50:19
这不是一种很好的做法,也是一种矫饰。相反,只需编写一个函数来执行您想要的操作,并在两个视图中使用它。
发布于 2019-08-10 04:00:04
我以前也有过类似的情况,通过遵循官方存储库中的这个issue,将我的DRF View
更改为ViewSet
解决了我的问题。只要尝试修改视图以从ViewSet
或GenericViewSet
或其他东西扩展即可。
https://stackoverflow.com/questions/57436340
复制相似问题