Skip to content
home_cover_image

Django Ninja - 快速的 Django REST 框架

Django Ninja 是一个无缝集成 Django, 使用 Python 3.6+ 类型提示构建 API 的网络框架。

教程 How-to 系列 了解更多

教程

本教程向您展示如何使用 Django Ninja 的大部分功能。本教程假定您至少了解 Django 框架的一些基础知识,例如如何创建项目并运行它。

How-to 系列

包括如何解析传参,处理响应,自定义路由,鉴权,自定义 API 文档,对异步的支持等等。

了解更多

Django Ninja 的开发的动机,基准测试,如何上手,快速示例和交互式 API 文档等。

Django Ninja is a web framework for building APIs with Django and Python 3.6+ type hints.

Main features:

  • Easy to understand: Designed to be easy to use and intuitive.
  • Fast execution: Thanks to Pydantic and async support, it has very high performance.
  • Fast to code: Django Ninja is a web framework for building APIs with Django and Python 3.6+ type hints.
  • Standards-based: Based on standards: OpenAPI (formerly Swagger) and JSON Schema.
  • Django friendly: (obviously) integrates well with Django core and the ORM.
  • Production ready: Used by multiple companies in real projects (if you use Django Ninja and want to publish your feedback, please email ppr.vitaly@gmail.com).

Benchmarks:

Django Ninja REST Framework

Installation

pip install django-ninja

Quick example

Start a new Django project (or use an existing one)

django-admin startproject apidemo

In urls.py

from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI

api = NinjaAPI()


@api.get("/add")
def add(request, a: int, b: int):
    return {"result": a + b}


urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/", api.urls),
]

Now, run it as usual:

./manage.py runserver

Note: you don't have to add Django Ninja to your installed apps for it to work.

Check it

Open your browser at http://127.0.0.1:8000/api/add?a=1&b=2

You will see the JSON response as:

{"result": 3}
Now you have just created an API that:

  • Receives an HTTP GET request at /api/add
  • Receives, validates, and type-converts GET parameters a and b
  • Decodes the result as JSON
  • Generates an OpenAPI schema for the defined operation

Interactive API documentation

Now go to http://127.0.0.1:8000/api/docs

You will see the automatic, interactive API documentation (provided by OpenAPI / Swagger UI or Redoc) :

Swagger UI

Summary

In short, you only declare the types of parameters, request bodies, etc. once, as function arguments.

You do this with standard, modern Python types.

You don't have to learn a new syntax, the methods or classes of a specific library, etc.

Just standard Python 3.6+.

For example, for an int:

a: int

Or, for a more complex Item model:

class Item(Schema):
    foo: str
    bar: float

def operation(a: Item):
    ...

...with just this one declaration, you get:

  • Editor support, including:
    • Autocompletion
    • Type checking
  • Data validation:
    • Automatic and clear errors when data is invalid
    • Validation even for deeply nested JSON objects
  • Serialization of network input data and conversion to Python data output, and reading from:
    • JSON
    • Path parameters
    • Query parameters
    • Cookies
    • Headers
    • Forms
    • Files
  • Automatic, interactive API documentation

This project was heavily inspired by FastAPI.

Note

Check out the Tutorial to get started quickly.

Ecosystem around Django Ninja:

  1. django-ninja: A framework based on Django that takes the best of Django and pairs it with the power of Pydantic — the optimal choice at the intersection of security and convenience.

  2. django-ninja-crud: A framework built on django-ninja to simplify CRUD interface development, providing declarative model viewsets and scenario-based testing methods.

  3. django-ninja-extra: A tool that lets developers skip verbose Django documentation and quickly build powerful web applications and modern API interfaces with minimal code.

  4. django-ninja-jwt: A JSON Web Token plugin for django-ninja that implements authentication and related features, forked from a popular Django Rest Framework plugin.


本文总阅读量次