动机
TIP
Django Ninja 看起来与 FastAPI 基本相同,那么为什么不直接使用 FastAPI 呢?
事实上,Django Ninja 深受 FastAPI(由 Sebastián Ramírez 开发)的启发。
也就是说,让 FastAPI 和 Django 正常协作时几乎没有问题:
FastAPI declares to be ORM agnostic (meaning you can use it with SQLAlchemy or the Django ORM), but in reality the Django ORM is not yet ready for async use (it may be in version 4.0 or 4.1), and if you use it in sync mode, you can have a closed connection issue which you will have to overcome with a lot of effort.
FastAPI 声明与 ORM 无关(意味着你可以将其与 SQLAlchemy 或 Django ORM一起使用),但实际上Django ORM 尚未准备好异步使用(可能在4.0或4.1版本中会准备好),如果在同步模式下使用它,你可能会遇到连接关闭问题,不得不付出很大的努力来克服这一问题。
The dependency injection with arguments makes your code too verbose when you rely on authentication and database sessions in your operations (which for some projects is about 99% of all operations).
...
app = FastAPI()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
async def get_current_user(token: str = Depends(oauth2_scheme)):
user = decode(token)
if not user:
raise HTTPException(...)
return user
@app.get("/task/{task_id}", response_model=Task)
def read_user(
task_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
... use db with current_user ....
- Since the word
modelin Django is "reserved" for use by the ORM, it becomes very confusing when you mix the Django ORM with Pydantic/FastAPI model naming conventions.
Django Ninja
Django Ninja addresses all those issues, and integrates very well with Django (ORM, urls, views, auth and more)
Working at Code-on a Django webdesign webedevelopment studio I get all sorts of challenges and to solve these I started Django-Ninja in 2020.
Note: Django Ninja is a production ready project - my estimation is at this time already 100+ companies using it in production and 500 new developers joining every month.
Some companies are already looking for developers with django ninja experience.
Main Features
- Since you can have multiple Django Ninja API instances - you can run multiple API versions inside one Django project.
api_v1 = NinjaAPI(version='1.0', auth=token_auth)
...
api_v2 = NinjaAPI(version='2.0', auth=token_auth)
...
api_private = NinjaAPI(auth=session_auth, urls_namespace='private_api')
...
urlpatterns = [
...
path('api/v1/', api_v1.urls),
path('api/v2/', api_v2.urls),
path('internal-api/', api_private.urls),
]
- The Django Ninja 'Schema' class is integrated with the ORM, so you can serialize querysets or ORM objects:
@api.get("/tasks", response=List[TaskSchema])
def tasks(request):
return Task.objects.all()
@api.get("/tasks", response=TaskSchema)
def tasks_details(request):
task = Task.objects.first()
return task
Instead of dependency arguments, Django Ninja uses
requestinstance attributes (in the same way as regular Django views) - more detail at Authentication.






