How to use a router in Django REST not for views, but for general views?

I have a very simple question about DjangoREST and the router.

When I need to register rest_framework.viewsets, I do something like this (example from the document):

router = routers.SimpleRouter() router.register(r'users', UserViewSet) router.register(r'accounts', AccountViewSet) 

But how to add to the router views which are from the rest_framework.generics package? Should I configure my own router ( http://www.django-rest-framework.org/api-guide/routers/#custom-routers )? What is the best practice?

+7
source share
2 answers

But how to add rest_framework.generics to the router views?

No. ViewSets adds a couple of compatibility levels to rest_framework.generics for working with routers.

Should I set up a custom router ( http://www.django-rest-framework.org/api-guide/routers/#custom-routers )? What is the best practice?

If you want to use a view without views, you won’t write a regular Django URL.

I feel that the real question is completely different, and there will be something like: "How to limit viewing to only certain actions."

In this case, the ModelViewSet gives the answer:

 class ViewSet(ViewSetMixin, views.APIView): """ The base ViewSet class does not provide any actions by default. """ pass class GenericViewSet(ViewSetMixin, generics.GenericAPIView): """ The GenericViewSet class does not provide any actions by default, but does include the base set of generic view behavior, such as the `get_object` and `get_queryset` methods. """ pass class ModelViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, GenericViewSet): """ A viewset that provides default `create()`, `retrieve()`, `update()`, `partial_update()`, `destroy()` and `list()` actions. """ pass 

As you can see, you can specialize ModelViewSet by selecting the necessary mixes and inheriting from GenericViewSet .

+4
source

In the case of generic views, they do not use routers, because they are class-based views of the source- drf documentation for class-based views

0
source

All Articles