class
Analyzer::Python::DjangoNinja
Overview
Django Ninja is a FastAPI-inspired REST framework that plugs into
Django's URL system. Operations are declared with decorators on a
NinjaAPI() (or Router()) instance — @api.get("/items") — and
the whole API is mounted through Django's URLconf:
api.py
from ninja import NinjaAPI, Router api = NinjaAPI()
@api.get("/add") def add(request, a: int, b: int): ...
router = Router() @router.get("/{event_id}") def event(request, event_id: int): ... api.add_router("/events/", router)
urls.py
urlpatterns = [path("api/", api.urls)]
The full URL is mount_prefix + router_prefix + operation_path, so
the analyzer works in phases: collect NinjaAPI/Router instances
and their operations, resolve each API's mount prefix from the
URLconf, resolve router prefixes from add_router(...), then emit
one endpoint per (instance, prefix) reachable from an API root.
Defined in:
analyzer/analyzers/python/django_ninja.crConstant Summary
-
ADD_ROUTER_CALL_RE =
/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*\.\s*add_router\s*\((.*)\)\s*(?:#.*)?$/m -
DECORATOR_RE =
/^\s*@\s*([A-Za-z_][A-Za-z0-9_]*)\s*\.\s*([A-Za-z_][A-Za-z0-9_]*)\s*\((.*)\)\s*(?:#.*)?$/m -
HTTP_DECORATOR_METHODS =
["get", "post", "put", "patch", "delete", "head", "options"] of ::String -
Verbs exposed as
@api.<verb>(...)decorators.api_operation(an explicit method list) is handled separately. -
KIND_API =
0 -
KIND_ROUTER =
1 -
MOUNT_STR_RE =
/\b(?:path|re_path|url)\s*\(\s*[rf]?['"]([^'"]*)['"]\s*,\s*[rf]?['"]([A-Za-z_][A-Za-z0-9_.]*)\.urls['"]/ -
path("api/", "myproject.api.api.urls")— the dotted-string form. -
MOUNT_VAR_RE =
/\b(?:path|re_path|url)\s*\(\s*[rf]?['"]([^'"]*)['"]\s*,\s*([A-Za-z_][A-Za-z0-9_]*)\.urls\b/ -
path("api/", api.urls)/re_path(r"^api/", api.urls)— a router instance mounted by attribute reference. -
NINJA_API_DECL_RE =
/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*(?::[^=]+)?=\s*(?:[A-Za-z_][A-Za-z0-9_.]*\.)?NinjaAPI\s*\(/ -
NINJA_ROUTER_DECL_RE =
/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*(?::[^=]+)?=\s*(?:[A-Za-z_][A-Za-z0-9_.]*\.)?Router\s*\(/ -
PATH_PARAM_REGEX =
/\{(?:[A-Za-z_][A-Za-z0-9_]*\s*:\s*)?([A-Za-z_][A-Za-z0-9_]*)\}/ -
django-ninja path parameters use Django's converter syntax, where the converter (when present) comes BEFORE the name:
{item_id}or{int:item_id}. This is the reverse of FastAPI's{item_id:int}, so the capture takes the token AFTER an optionalconverter:. -
SCALAR_TYPES =
["str", "int", "float", "bool", "bytes", "date", "datetime", "time", "UUID", "uuid", "EmailStr", "Decimal"] of ::String -
Scalar annotations that, absent an explicit marker (
Query(...),Body(...), ...), map a handler parameter to a query parameter.