I use Django 2.1 and DRF and a planning a quite larger application with many plugged-in apps. I'd like to have ONE /api URL for the DRF as endpoint, but allow each app to have a special model exposed over the REST endpoint, e.g.:
In the main urls.py:
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
urlpatterns = [
# ...
path('api/', include(router.urls)),
# ...
]
and in foo_app/urls.py:
router = routers.DefaultRouter()
router.register(r'foomodel', FooModelViewSet)
Now, /api/foomodel produces a 404 Error. The foo_model/urls.py gets imported (a print statement there is printed at Django start), and all the other foo_model.urlpattern[path...] are recognized and work fine.
How can I define custom api model endpoints for a central api REST endpoint? I didn't find anything in the documentation.
Thanks in advance.
I simply achieved this by extending the router registries.
root urls.py
from ad.urls import router as ad_router
main_router = routers.DefaultRouter()
main_router.registry.extend(ad_router.registry)
ad.urls.py
from .api.urls import router as api_router
router = routers.DefaultRouter()
router.registry.extend(api_router.registry)
ad.api.urls.py
router = routers.DefaultRouter()
router.register(r'ad', AdViewSet)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With