name: django-ninja-skills description: Expert guidance for high-performance Django API development using Django Ninja.
Django Ninja Skills
Overview
This skill provides expert-level guidance for building RESTful APIs using Python 3.10+ type hints and Pydantic. It focuses on leveraging Django Ninja's core strengths: Asynchronous support (Async), Automatic OpenAPI/Swagger generation, and Type-safe data validation.
Core Competencies
1. Input Parsing & Validation:
- Parameter Handling: Using type hints for Path, Query, Header, and Cookie parameters.
- Request Body: Defining complex payloads using Pydantic Schemas.
- Data Ingestion: Managing Form data, file uploads, and multi-part requests.
2. Response Serialization:
- Schema Modeling: Utilizing
ModelSchemafor direct Django model mapping. - Response Control: Defining status-code-specific responses (e.g.,
{201: SuccessOut, 403: ErrorOut}). - Optimization: Implementing built-in Pagination and custom Response Renderers.
3. API Architecture:
- Modular Routing: Using
Routerto decouple business domains. - Dependency Injection: Implementing reusable authentication and logic injection.
Instructions
- Schema-First Design: Always define Pydantic Schemas for data exchange. Use
ModelSchemato map Django models, explicitly controlling field exposure viamodel_fieldsormodel_exclude. - Async Integration: Prefer
async deffor I/O-bound view functions, ensuring compatibility with Django’s asynchronous ORM features. - Standardized Error Handling: Avoid manual JSON error responses. Use
ninja.errors.HttpErroror register globalexception_handlersfor consistent API error contracts. - Injected Security: Do not manually verify users within views. Use the
authparameter inRouterorNinjaAPIto enforce authentication via dependency injection. - Documentation Standards: Every endpoint must include a docstring and use
tagsto maintain a clean and navigable Swagger UI.
Best Practices
- Naming: Suffix schema classes with
InorOut(e.g.,UserIn,UserOut) to clarify data flow. - Status Codes: Be explicit with HTTP status codes (e.g., return
201for successful creation). - Logic Separation: Keep view functions thin; move complex business logic to a dedicated
services.pylayer.
Reference Templates
Refer to snippets/schema.py for the ModelSchema definitions and field naming.
- Standard Pydantic Schema (Non-Model based)
- Model-based Schemas
- Advanced Filtering/Search Schema
- Auth/Token Schemas
Refer to snippets/crud_async.py for the standard implementation of:
- Async CRUD operations.
- Multi-status code handling (201, 204, 404).
- Integration with Django 5.0+ Async ORM.
Refer to snippets/auth.py Implementations for APIKey, JWT, and dependency-based security.
- API Key Authentication (e.g., for Internal Service-to-Service)
- JWT / Bearer Token Authentication (Standard Web Auth)
- Role-Based Access Control (RBAC) Pattern
Refer to snippets/ninja_jwt.py for integration with django-ninja-jwt.
- Detailed
NINJA_JWTconfiguration (Rotation, Blacklisting). - Synchronous and Asynchronous JWT authentication patterns.
- Custom JWT-based API Key header implementations.
Refer to snippets/csrf.py for CSRF protection patterns in Django Ninja.
- CSRF exemption and validation patterns.
- Integration with frontend CSRF tokens.
Refer to snippets/comparison.py for comparisons between anti-patterns and optimized code.
- Response Handling
- Async Usage
- Error Handling
- Authentication
Refer to snippets/pagination.py for apply pagination to a function - just apply paginate decorator:
- Default (LimitOffset): @paginate
- Page Number: @paginate(PageNumberPagination)
- Cursor: @paginate(CursorPagination)
- Custom: Create a class inheriting from PaginationBase and use @paginate(CustomPagination)
Refer to snippets/throttles.py for implementing API rate limiting and throttling:
- Global Throttling
- Router-Level Throttling
- Endpoint-Level Throttling
- 💡 Rate Format Guide
- 🛠️ How to Test Your Rate Limiting?
Expert Next.js App Router
Developpement
Un skill qui transforme Claude en expert Next.js App Router.
Générateur de README
Developpement
Crée des README.md professionnels et complets pour vos projets.
Rédacteur de Documentation API
Developpement
Génère de la documentation API complète au format OpenAPI/Swagger.