How to use router in Django REST not for viewsets, but for generic views?
But how to add to router views that are from rest_framework.generics package?
You don't. ViewSets adds a couple of compatibility layer to rest_framework.generics
in order to work with routers.
Should I bild custom router (http://www.django-rest-framework.org/api-guide/routers/#custom-routers)? What is the best practice?
If you want to use a non viewset view, you'll be down to writing the regular Django url.
My feeling is the real question is quite different and would be something like, "how do I restrict viewset to some actions only".
In which case, the declaration of the ModelViewSet
provides 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 the ModelViewSet
by selecting the required mixins and inheriting from GenericViewSet
.