Django Shinobi - Fast Django REST Framework
Django Shinobi is a web framework for building APIs with Django and Python 3.6+ type hints.
It's a fork of the fantastic Django Ninja library focused on community-desired features and fixes. Check out the list of differences if you're coming from Ninja, as well as the roadmap!
Key features:
- Easy: Designed to be easy to use and intuitive.
- FAST execution: Very high performance thanks to Pydantic and async support.
- Fast to code: Type hints and automatic docs lets you focus only on business logic.
- Standards-based: Based on the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
- Django friendly: (obviously) has good integration with the Django core and ORM.
- Production ready: The original Ninja project is used by multiple companies on live projects.
Installation
In your Django project, add Django Shinobi.
pip install django-shinobi
or start a new project.
pip install django django-shinobi
django-admin startproject apidemo
Usage
In your Django project, next to urls.py, create a new file called api.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 go to urls.py
and add the following:
...
from .api import api
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", api.urls), # <---------- !
]
That's it !
Now you've just created an API that:
- receives an HTTP GET request at
/api/add
- takes, validates and type-casts GET parameters
a
andb
- decodes the result to JSON
- generates an OpenAPI schema for defined operation
Interactive API docs
Run your Django project
python manage.py runsever
Now go to http://127.0.0.1:8000/api/docs
You will see the automatic interactive API documentation (provided by Swagger UI or Redoc):
Recap
In summary, you declare the types of parameters, body, etc. once only, as function parameters.
You do that 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):
...
... and with that single declaration you get:
- Editor support, including:
- Completion
- Type checks
- Validation of data:
- Automatic and clear errors when the data is invalid
- Validation, even for deeply nested JSON objects
- Conversion of input data coming from the network, to Python data and types, and reading from:
- JSON
- Path parameters
- Query parameters
- Cookies
- Headers
- Forms
- Files
- Automatic, interactive API documentation
What next?
- Read the full documentation here - https://pmdevita.github.io/django-shinobi
- To support this project, please give star it on Github.
- Share it via Twitter
- Share your feedback and discuss development on Discord https://discord.gg/ntFTXu7NNv