Django REST Framework combining routers from different apps

前端 未结 6 1850
面向向阳花
面向向阳花 2020-12-12 14:33

I have a project that spans multiple apps:

./project/app1
./project/app2
./project/...

Each app has a router for Django REST Framework to i

6条回答
  •  囚心锁ツ
    2020-12-12 14:54

    If you are implementing a SimpleRouter you just concatenate its urls with the urlpatterns list

    router = SimpleRouter()
    router.register(r'import-project', ImportProject, base_name='di-integ')
    

    On the main urls.py file

    from di_apiary_integration.urls import router as di_integration_routers
    

    To register the URL's you can do:

    url(r'^di-integration/', include(di_integration_routers.urls)),
    

    or

    urlpatterns += di_integ_router.urls
    

    Both will work!

    Important

    ImportProject needs to be either a ModelViewSet or a ViewSet, if you create this as simple APIView you will need to register that as normal CBV View using as_view()

提交回复
热议问题