Django REST API Best Practices
Comprehensive guide for Django and Django REST Framework development. Contains rules prioritized by impact on database performance, response times, and security.
When to Apply
Reference these guidelines when:
- •Designing or modifying Django models and database schemas (PostgreSQL).
- •Implementing Django REST Framework serializers and data validation logic.
- •Developing API views using Django REST Framework ViewSets or Generic Views.
- •Optimizing database interactions using the Django ORM (e.g.,
select_related,prefetch_related). - •Managing authentication flows, custom permissions, and throttling policies.
- •Writing custom Django management commands for administrative tasks.
- •Implementing middleware for global request or response processing.
- •Handling Django signals and receivers for decoupled event logic.
- •Structuring new applications and defining dependencies within the Django project.
- •Customizing the Django Admin interface for internal data management.
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Architecture & Design Patterns | HIGH | arch- |
| 2 | Project Configuration & Tooling | MEDIUM | conf- |
Quick Reference
1. Architecture & Data Integrity (HIGH)
- •
arch-app-structure- Enforcesapps/directory and package-based models/views - •
arch-orm-model-structure- One model per file, strict meta options, and typing - •
arch-api-serializer-definition- Action-based naming and representation delegation
2. Configuration & DevOps (MEDIUM)
- •
conf-settings-structure- Modular settings (base/development/production) and secure secrets - •
conf-env-dependencies- Segregated.venvand split requirement files - •
conf-docker-standards- Slim images, layer caching, and reverse-domain naming - •
arch-view-definition- Generic views usage and strict APIView typing
How to Use
Read individual rule files for detailed explanations and code examples:
code
./rules/*.md
Each rule file contains:
- •Brief explanation of why it matters
- •Incorrect code example with explanation
- •Correct code example with explanation
- •Additional context and references
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md