如何将一个包含APIRouter在另一个中并指定某个路径参数是必需的前缀?
对于上下文:\n假设我有组织和用户的概念。一个用户只属于一个组织。我的网络应用程序的结构如下:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 web_app\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 endpoints\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py # produces main_router by including other routers inside each other\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 organization.py # contains endpoints relevant to the organization\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 user.py # contains endpoints relevant to the user\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.py # includes main_router in app\nRun Code Online (Sandbox Code Playgroud)\n假设我想为组织和用户实现基本的 CRUD 功能。我的端点可能看起来像这样:
\n对于组织:
\nGET /api/latest/org/{org_id}\nPOST /api/latest/org/{org_id}\nPUT /api/latest/org/{org_id}\nDELETE /api/latest/org/{org_id}\nRun Code Online (Sandbox Code Playgroud)\n对于用户:
\nGET /api/latest/org/{org_id}/users/{user_id}\nPOST /api/latest/org/{org_id}/users/{user_id}\nPUT /api/latest/org/{org_id}/users/{user_id}\nDELETE /api/latest/org/{org_id}/users/{user_id}\nRun Code Online (Sandbox Code Playgroud)\n由于用户嵌套在 orgs 下,因此user.py我可以像这样编写所有端点:
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 web_app\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 endpoints\n …Run Code Online (Sandbox Code Playgroud)