'How to Nest URL routes in Django Rest Framework (DRF) RetrieveUpdateDestroyAPIView without router

I am using the Django rest framework and RetrieveUpdateDestroyAPIView and trying to Implement nested URL patterns.

# my current URLs structure is like
/api/v1/kitchens/ - ListCreateAPIView
/api/v1/kitchens/1/ - RetrieveUpdateDestroyAPIView
/api/v1/orders/ - ListCreateAPIView
/api/v1/orders/1/ - RetrieveUpdateDestroyAPIView
/api/v1/items/ - ListCreateAPIView
/api/v1/items/1/ - RetrieveUpdateDestroyAPIView

The above structure works fine with all the operations but to get the list of items or orders under a single kitchen I am trying to achieve something live below.

the views.py has something like below.

class KitchenListView(generics.ListCreateAPIView):
  queryset = Kitchen.objects.all()
  serializer_class = KitchenSerializer
  pagination_class = LimitOffsetPagination
  permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
  authentication_classes = (TokenAuthentication,)
  filter_backends = (DjangoFilterBackend,)
  filter_fields = ("user",)


class KitchenDetailView(generics.RetrieveUpdateDestroyAPIView):
  queryset = Kitchen.objects.all()
  serializer_class = KitchenDetailsSerializer
  permission_classes = (
    permissions.IsAuthenticatedOrReadOnly,
    IsOwnerOrReadOnly,
  )
 authentication_classes = (TokenAuthentication,)

# I want to create this structure.
/api/v1/kitchens/ - List
/api/v1/kitchens/1/ - Details
/api/v1/kitchens/1/orders/ - Orders List
/api/v1/kitchens/1/orders/1/ - Order Details
/api/v1/kitchens/1/items/ - Items List
/api/v1/kitchens/1/items/1/ - Item Details

I am trying to achieve something like this. Please suggest to me the better solution if anyone has and what would be better to go with generic API views or Viewsets as my project is a large-scale project.

Thanks in Advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source