vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.36k stars 437 forks source link

[BUG] docs doesn't work well in multi apps and each "api.py"... #450

Open sikyeong opened 2 years ago

sikyeong commented 2 years ago

Describe the bug A clear and concise description of what the bug is.

I have two APPS in my Django project. And create each api.py in each apps

  1. masters
    • api.py
  2. boxs
    • api.py

and my project "urls.py" is like this


from django.contrib import admin from django.urls import path from masters.api import masters #masters = NinjaAPI() from boxs.api import boxs #boxs = NinjaAPI()

urlpatterns = [ path('admin/', admin.site.urls), path('boxs/', boxs.urls), path('masters/', masters.urls), ]


then, i want's open each api docs so try to call like this

  1. [GET] http://127.0.0.1:8000/masters/docs
  2. [GET] http://127.0.0.1:8000/boxs/docs

but' i can open just one docs of both (the urls response only "/boxs/openapi.json") [18/May/2022 23:42:42] "GET /masters/docs HTTP/1.1" 200 801 [18/May/2022 23:42:42] "GET /boxs/openapi.json HTTP/1.1" 200 298

when i change my project "urls.py" like this


from django.contrib import admin from django.urls import path from masters.api import masters from boxs.api import boxs

urlpatterns = [ path('admin/', admin.site.urls), path('masters/', masters.urls), path('boxs/', boxs.urls),

]


the urls response only "masters/openapi.json" [18/May/2022 23:59:12] "GET /boxs/docs HTTP/1.1" 200 792 [18/May/2022 23:59:12] "GET /masters/openapi.json HTTP/1.1" 200 744

thanks

Versions (please complete the following information):

vitalik commented 2 years ago

@sikyeong

try giving each NinjaAPI insstnace own urlnamespace ? NinjaAPI(urls_namespace="masters")

quroom commented 2 years ago

Use django-ninja-extra for class based api. It makes easier to implement multiple app's api.

  1. Mkae resuable api = NinjaExtraAPI in project api.py file
  2. make each controlles in each app api.py file.
  3. register each controller in project api.py file

It's more efficient structure for big project. because there are many apps in same project.

adonig commented 1 year ago

A general note for people who come here because they run into a namespace resolution problem:

You might have to prefix your API namespace in case you are nesting your Ninja API within an existing app namespace.

api = NinjaAPI(urls_namespace="foo:bar")