2015-05-20 27 views
6

考慮簡單的觀點:嵌套detail_route

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) 

我打電話SomeOtherView必須有一個終結點屬性一樣/someresource/:id/myproperty能力,所以這個屬性將接受請求,並可以做所有CRUD動作。

但是,我想SomeOtherView也有內部申報detail_route有像/someresource/:id/myproperty/nestedproperty
由於我動態調用SomeOtherView,因此無法註冊URL,因此無法調用嵌套屬性。

我如何解決這種情況具有嵌套屬性?

回答

3

目前在自動創建在Django的休息框架嵌套的路線沒有原生的方式,但也有一些方式來實現自己的目標:

  1. 使用DRF-一些推廣,什麼你正在尋找嵌套路由器:https://chibisov.github.io/drf-extensions/docs/#nested-routes
  2. 手動創建的路徑與默認的路由器,在這裏你需要過濾你的查詢集手動

你雖然沒有詳細要使用此API結構才達到我不會RECO解釋什麼繼續這條道路,因爲意見並不打算像那樣使用。