Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested detail_route in django-rest-framework

Consider simple view:

class SomeView(viewsets.GenericViewSet,
               viewsets.mixins.ListModelMixin,
               viewsets.mixins.RetrieveModelMixin):
    ...

    @decorators.detail_route(methods=ENDPOINT_PROPERTY_METHODS)
    def some_property(self, request, *args, **kwargs):
      view = SomeOtherView
      return view.as_view(CRUD_ACTIONS)(request, *args, **kwargs)

I'm calling SomeOtherView to have ability to have an endpoint-property like /someresource/:id/myproperty, so this property will receive request and can do all CRUD actions.

But, I want to SomeOtherView to have the declared detail_route inside too to have something like /someresource/:id/myproperty/nestedproperty.
Since I'm calling SomeOtherView dynamically, urls can not be registered, so nested property can not be called.

How I can resolve such situation to have nested properties?

like image 542
coldmind Avatar asked May 20 '15 13:05

coldmind


1 Answers

There is currently no native way in automatically creating nested routes in django-rest-framework but there are some ways to achieve your goal:

  1. use drf-extentions, what your are searching for are nested routers: https://chibisov.github.io/drf-extensions/docs/#nested-routes
  2. create the paths manually with the default routers, here you need to filter your queryset manually

Although you didn't explain in detail what you want to achive with this api structure I wouldn't recomment continuing this path because views are not intended to be used like that.

like image 62
Johannes Reichard Avatar answered Sep 29 '22 07:09

Johannes Reichard