AgentSkillsCN

django-rest-framework-best-practices

Django 与 Django REST Framework 优化指南。本技能明确了后端架构与性能的标准,重点关注 ORM 效率、序列化策略以及 API 安全性。

SKILL.md
--- frontmatter
name: django-rest-framework-best-practices
description: Django and Django REST Framework optimization guidelines. This skill defines the architectural and performance standards for the back-end, focusing on ORM efficiency, serialization strategies, and API security
license: MIT
metadata:
  author: gianllopez
  version: 1.0.0

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

PriorityCategoryImpactPrefix
1Architecture & Design PatternsHIGHarch-
2Project Configuration & ToolingMEDIUMconf-

Quick Reference

1. Architecture & Data Integrity (HIGH)

  • arch-app-structure - Enforces apps/ 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 .venv and 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