
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Django Ninja Extra - Class Based Utility and more for Django Ninja(Fast Django REST framework)
Django Ninja Extra is a powerful extension for Django Ninja that enhances your Django REST API development experience. It introduces class-based views and advanced features while maintaining the high performance and simplicity of Django Ninja. Whether you're building a small API or a large-scale application, Django Ninja Extra provides the tools you need for clean, maintainable, and efficient API development.
🏗️ Class-Based Controllers:
🔒 Advanced Permission System (Similar to Django Rest Framework):
💉 Dependency Injection:
🔧 Service Layer:
pip install django-ninja-extra
INSTALLED_APPS = [
...,
'ninja_extra',
]
Create api.py
in your Django project:
from ninja_extra import NinjaExtraAPI, api_controller, http_get
api = NinjaExtraAPI()
# Function-based endpoint example
@api.get("/hello", tags=['Basic'])
def hello(request, name: str = "World"):
return {"message": f"Hello, {name}!"}
# Class-based controller example
@api_controller('/math', tags=['Math'])
class MathController:
@http_get('/add')
def add(self, a: int, b: int):
"""Add two numbers"""
return {"result": a + b}
@http_get('/multiply')
def multiply(self, a: int, b: int):
"""Multiply two numbers"""
return {"result": a * b}
# Register your controllers
api.register_controllers(MathController)
In urls.py
:
from django.urls import path
from .api import api
urlpatterns = [
path("api/", api.urls), # This will mount your API at /api/
]
from ninja_extra import api_controller, http_get
from ninja_extra.permissions import IsAuthenticated, PermissionBase
# Custom permission
class IsAdmin(PermissionBase):
def has_permission(self, context):
return context.request.user.is_staff
@api_controller('/admin', tags=['Admin'], permissions=[IsAuthenticated, IsAdmin])
class AdminController:
@http_get('/stats')
def get_stats(self):
return {"status": "admin only data"}
@http_get('/public', permissions=[]) # Override to make public
def public_stats(self):
return {"status": "public data"}
from injector import inject
from ninja_extra import api_controller, http_get
# Service class
class UserService:
def get_user_details(self, user_id: int):
return {"user_id": user_id, "status": "active"}
# Controller with dependency injection
@api_controller('/users', tags=['Users'])
class UserController:
def __init__(self, user_service: UserService):
self.user_service = user_service
@http_get('/{user_id}')
def get_user(self, user_id: int):
return self.user_service.get_user_details(user_id)
Access your API's interactive documentation at /api/docs
:
We welcome contributions! Here's how you can help:
Please ensure your code follows our coding standards and includes appropriate tests.
This project is licensed under the MIT License - see the LICENSE file for details.
The service
attribute in ModelController
has been changed from a class object to an instance object. When creating a custom ModelService
for a ModelController
, you have to specify it as service_type
.
This is because services are now injected as dependencies during controller instantiation. Service instantiation is delegated to the injector package, so ensure that any additional dependencies required by your ModelService
are properly registered in the dependency injection container.
For more details, please refer to the documentation
FAQs
Django Ninja Extra - Class Based Utility and more for Django Ninja(Fast Django REST framework)
We found that django-ninja-extra demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.