Django Rest Framework, can I use ViewSet to generate json from django view function?

I know that I can use the drf serializer from django views, but the set of queries, setting up pagination are duplicated in drf and django views.

Is it possible to reuse a viewet to generate json data and include it in a regular django response?

Update:
those. is it possible to call ViewSet.as_view()(self.request) from a django view?
this is not documented, so I wonder about the lower bounds of this approach .. and if it is doable ..

+5
source share
1 answer

Yes, you can call YourViewSet.as_view () (self.request) in your Django view.

Make sure you call ViewSet as shown below:

YourViewSet.as_view ({'get': 'list'}) (self.request)

Otherwise it will throw an exception

The actions argument must be specified when .as_view() is called in the ViewSet. For example .as_view({'get': 'list'})

+2
source

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


All Articles