Django REST框架组合来自不同应用程序的路由器 [英] Django REST Framework combining routers from different apps

查看:159
本文介绍了Django REST框架组合来自不同应用程序的路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涵盖多个应用程序的项目:

I have a project that spans multiple apps:

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

每个应用程序都有一个用于Django REST Framework的路由器包含该应用程序提供的API的部分:

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)),
]

由于应用程序是分开的,我的顶级URL文件( ./ project / urls.py )包括来自不同应用程序的URL文件:

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')),

T他的意思是Django REST框架为每个应用程序显示了一个单独的API根。然而,我想要的是一个统一的API结构,所以如果我导航到 http://example.com/api/ ,我看到所有URL的完整列表我们假设有一种方法可以包含在单独的 urls.py中定义的所有单独的路由器。

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?

推荐答案

另一个解决方案是使用 SimpleRouter 为各个应用定义路由器。然后,使用自定义的 DefaultRouter 来包括应用程序特定的路由。这样,所有应用程序特定的url定义将保留在相应的应用程序中。

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.

让我们说你有两个名为app1和app2的应用程序名为api的目录,在该目录中有一个名为urls的文件,其中包含所有路由定义。

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.


├──项目/
│├──api_urls.py
│├──app1
││├──api
│││├──urls.py
│├──app2
││├──api
│││├──urls.py
│├──补丁
││├──路由器。 py

使用 patches / router.py 来定义一个类命名为 DefaultRouter 继承自 rest_framework.routers.DefaultRouter

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)

填充您的api urls路由定义,如

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)

这篇关于Django REST框架组合来自不同应用程序的路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆