I have a project that spans multiple apps:
./project/app1 ./project/app2 ./project/...
Each app has a router for Django REST Framework to incorporate the parts of the API provided by that app:
from django.conf.urls import url, include from rest_framework.routers import DefaultRouter from .views import ThingViewSet router = DefaultRouter() router.register(r'things', ThingViewSet, base_name='thing') urlpatterns = [ url(r'^', include(router.urls)), ]
Because the apps are separate, my top-level URLs file (./project/urls.py
) includes each of the URLs files from the separate apps:
url(r'^api/app1/', include('app1.urls', namespace='a1')), url(r'^api/app2/', include('app2.urls', namespace='a2')),
This means that Django REST Framework shows a separate API root for each app. What I would like, however, is a unified API structure, so that if I navigate to http://example.com/api/
I see the full list of all URLs that are available at that level of the hierarchy.
I presume there is a way to include all my separate routers defined in the individual urls.py
files for each app into a single router, but I can't find documentation on how to do this. Am I missing something obvious?
basename - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the queryset attribute of the viewset, if it has one. Note that if the viewset does not include a queryset attribute then you must set basename when registering the viewset.
Django Rest Framework makes it easy to use your Django Server as an REST API. REST stands for "representational state transfer" and API stands for application programming interface. Note that with DRF you easily have list and create views as well as authentication.
A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as . get() or . post() , and instead provides actions such as . list() and . create() .
Django Rest Framework is a powerful library that sits on top of existing Django projects to add robust web APIs. If you have an existing Django project with only models and a database--no views, urls, or templates required--you can quickly transform it into a RESTful API with a minimal amount of code.
Another solution is to use SimpleRouter
to define routers for individual apps. Then, use a customized DefaultRouter
to include app specific routes. This way all of the app specific url definitions will stay in the corresponding app.
Lets say you have two apps named "app1" and "app2" each of these apps have a directory named "api" and in this directory there is a file named "urls" that contain all your route definitions.
├── project/ │ ├── api_urls.py │ ├── app1 │ │ ├── api │ │ │ ├── urls.py │ ├── app2 │ │ ├── api │ │ │ ├── urls.py │ ├── patches │ │ ├── routers.py
use patches/router.py
to define a class named DefaultRouter
that inherits from rest_framework.routers.DefaultRouter
.
from rest_framework import routers class DefaultRouter(routers.DefaultRouter): """ Extends `DefaultRouter` class to add a method for extending url routes from another router. """ def extend(self, router): """ Extend the routes with url routes of the passed in router. Args: router: SimpleRouter instance containing route definitions. """ self.registry.extend(router.registry)
Fill your api urls with route definitions like
""" URL definitions for the api. """ from patches import routers from app1.api.urls import router as app1_router from app2.api.urls import router as app2_router router = routers.DefaultRouter() router.extend(app1_router) router.extend(app2_router)
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