How do i rewrite my code with Routers? Django rest framework

I have this REST API:

urlpatterns = [
    path('users/', UserViewSet.as_view({'get': 'list', 
                                        'post': 'create', 
                                        'delete': 'delete'})),
    path('users/<uuid:pk>/video/', UserViewSet.as_view({'post': 'video'}))

How can i rewrite this with routers?

Default router with register method creates API different from current.

Or, maybe, in this situation it would be more correct to use my code?

assuming that UserViewSet is indeed a viewset, you can use the restframework's default router to register the router for /users/, and then add an action to handle you /video/ route from that viewset.

from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'users/', UserViewSet, basename='users')

urlpatterns = [

urlpatterns += router.urls

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import (
    CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, ListModelMixin
from rest_framework.decorators import action
from rest_framework.parsers import MultiPartParser

class UserViewSet(CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, ListModelMixin, GenericViewSet):
    # serializer class
    # queryset
    # permissions

    @action(methods=['post'], parser_classes=(MultiPartParser,), detail=True)
    def video(self, request, pk=None, *args, **kwargs):
        # Implementation to upload a video
Back to Top