Rest Api Django

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Comprehensive guidelines for Django REST API development covering project structure, views, models, serializers, authentication, performance, and error handling.

AI 生成的概览

提供 Django REST Framework API 开发指南,涵盖项目结构、视图、模型、序列化器、认证、性能与错误处理。

功能
该技能提供使用 Django 与 Django REST Framework 开发 REST API 的参考指南。内容涵盖项目与 API 目录结构、基于类的视图、ORM 使用、序列化器与校验、JWT 认证与权限、预加载与缓存等性能实践,以及统一的错误响应格式。它产出的是规范与建议,而非代码或文件。
适用场景
在设计或实现 Django REST API,并需要关于结构、序列化器、认证、性能或错误处理的约定时使用。适合需要清单式参考的 Django 后端开发者。
运行要求
不包含脚本或工具,仅为说明性指导。应用这些指南需具备 Django 项目与 Django REST Framework;文中还提到 JWT 认证、Redis 或 Memcached 缓存以及 Celery 任务等可选组件。

REST API Development with Django

You are an expert in Django REST Framework for building scalable APIs.

Core Principles

  • Use Django's built-in features wherever possible
  • Prioritize readability following PEP 8 compliance
  • Use descriptive names with lowercase underscores
  • Structure projects using Django apps for reusability
  • Consider scalability in all design decisions

Project Structure

Application Structure

app_name/  migrations/       # Database migrations  admin.py          # Admin configuration  models.py         # Data models  managers.py       # Custom model managers  signals.py        # Django signals  tasks.py          # Celery tasks

API Structure

api/  v1/    urls.py         # URL routing    serializers.py  # DRF serializers    views.py        # API views    permissions.py  # Custom permissions    filters.py      # Query filters

Development Guidelines

Views and API Design

  • Use class-based views with DRF's APIViews
  • Follow RESTful principles for endpoint design
  • Keep business logic in models, not views
  • Maintain consistent response formats

Models and Database

  • Leverage Django ORM for all database operations
  • Use select_related() and prefetch_related() to prevent N+1 queries
  • Apply proper indexing on frequently queried fields
  • Use transaction.atomic() for critical operations

Serializers and Validation

  • Use DRF serializers for all data transformation
  • Implement custom validators for complex validation
  • Handle nested relationships properly
  • Keep serializers focused and composable

Authentication and Authorization

  • Use djangorestframework_simplejwt for JWT authentication
  • Implement granular permissions per endpoint
  • Ensure CSRF protection for session-based auth
  • Apply principle of least privilege

Performance Optimization

  • Prevent N+1 queries through eager loading
  • Implement database connection pooling
  • Use Redis or Memcached for caching
  • Apply standardized pagination to list endpoints

Error Handling

python
{    "success": False,    "message": "Validation failed",    "errors": {        "field_name": ["Error message"]    },    "error_code": "VALIDATION_ERROR"}
  • Use appropriate HTTP status codes
  • Return consistent error response structure
  • Apply structured logging for debugging
  • Never expose internal errors to clients

来源与署名

来源:mindrally/skills位于rest-api-django提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架