I know I can use drf serializer from django views, but queryset, pagination setting is all duplicated in drf viewset and django view.
我知道我可以使用django视图中的drf序列化程序,但是查询集,分页设置在drf viewset和django视图中都是重复的。
Can I reuse viewset to generate json data and include it in regular django response?
我可以重用viewset来生成json数据并将其包含在常规的django响应中吗?
Update:
ie, Can I call ViewSet.as_view()(self.request)
from django view?
it's not documented way, so I'm wondering the downsides of this approach .. and if it's doable..
更新:ie,我可以从django视图调用ViewSet.as_view()(self.request)吗?它没有记录的方式,所以我想知道这种方法的缺点..如果它是可行的..
1 个解决方案
#1
2
Yes, you can call YourViewSet.as_view()(self.request) in your Django view.
是的,您可以在Django视图中调用YourViewSet.as_view()(self.request)。
Make sure you call the ViewSet like below:
确保按以下方式调用ViewSet:
YourViewSet.as_view({'get': 'list'})(self.request)
Else it will raise an exception
否则它会引发异常
The
actions
argument must be provided when calling.as_view()
on a ViewSet. For example.as_view({'get': 'list'})
在ViewSet上调用.as_view()时必须提供actions参数。例如.as_view({'get':'list'})
#1
2
Yes, you can call YourViewSet.as_view()(self.request) in your Django view.
是的,您可以在Django视图中调用YourViewSet.as_view()(self.request)。
Make sure you call the ViewSet like below:
确保按以下方式调用ViewSet:
YourViewSet.as_view({'get': 'list'})(self.request)
Else it will raise an exception
否则它会引发异常
The
actions
argument must be provided when calling.as_view()
on a ViewSet. For example.as_view({'get': 'list'})
在ViewSet上调用.as_view()时必须提供actions参数。例如.as_view({'get':'list'})