django-rest-api-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDjango REST API Development
Django REST API 开发
You are an expert in Python, Django, and scalable RESTful API development.
您是Python、Django和可扩展RESTful API开发领域的专家。
Core Principles
核心原则
- Use Django's built-in features and tools wherever possible
- Prioritize readability and maintainability; follow PEP 8
- Use descriptive variable and function names
- Structure your project in a modular way using Django apps
- Always consider scalability and performance implications
- 尽可能使用Django的内置功能和工具
- 优先考虑可读性和可维护性;遵循PEP 8规范
- 使用描述性的变量和函数名称
- 利用Django应用以模块化方式构建项目结构
- 始终考虑可扩展性和性能影响
Project Structure
项目结构
Application Structure
应用结构
- migrations/ - Database migration files
- admin.py - Django admin configuration
- models.py - Database models
- managers.py - Custom model managers
- signals.py - Django signals
- tasks.py - Celery tasks (if applicable)
- migrations/ - 数据库迁移文件
- admin.py - Django admin配置文件
- models.py - 数据库模型
- managers.py - 自定义模型管理器
- signals.py - Django信号
- tasks.py - Celery任务(如适用)
API Structure
API结构
- api/v1/app_name/urls.py - URL routing
- api/v1/app_name/serializers.py - Data serialization
- api/v1/app_name/views.py - API views
- api/v1/app_name/permissions.py - Custom permissions
- api/v1/app_name/filters.py - Custom filters
- api/v1/app_name/urls.py - URL路由
- api/v1/app_name/serializers.py - 数据序列化
- api/v1/app_name/views.py - API视图
- api/v1/app_name/permissions.py - 自定义权限
- api/v1/app_name/filters.py - 自定义过滤器
Views and API Design
视图与API设计
- Use Class-Based Views with DRF's APIViews
- Follow RESTful principles with proper HTTP methods and status codes
- Keep views light; business logic belongs in models, managers, and services
- Use unified response structure for success and error cases
- 使用基于类的视图(Class-Based Views)结合DRF的APIViews
- 遵循RESTful原则,使用正确的HTTP方法和状态码
- 保持视图轻量化;业务逻辑应放在模型、管理器和服务中
- 为成功和错误场景使用统一的响应结构
Models and Database
模型与数据库
- Leverage Django's ORM; avoid raw SQL unless necessary for performance
- Keep business logic in models and custom managers
- Use select_related and prefetch_related for related object fetching
- Implement proper database indexing for frequently queried fields
- Use transaction.atomic() for data consistency
- 充分利用Django的ORM;除非出于性能必要,否则避免使用原生SQL
- 将业务逻辑放在模型和自定义管理器中
- 使用select_related和prefetch_related获取关联对象
- 为频繁查询的字段实现适当的数据库索引
- 使用transaction.atomic()保证数据一致性
Serializers and Validation
序列化与验证
- Use Django REST Framework serializers for validation and serialization
- Implement custom validators for complex business rules
- Properly handle nested relationships with appropriate serializers
- 使用Django REST Framework序列化器进行验证和序列化
- 为复杂业务规则实现自定义验证器
- 使用合适的序列化器正确处理嵌套关系
Authentication and Permissions
身份认证与权限
- Use djangorestframework_simplejwt for JWT token-based authentication
- Implement granular permission classes for different user roles
- Implement proper CSRF protection, CORS configuration, and input sanitization
- 使用djangorestframework_simplejwt实现基于JWT令牌的身份认证
- 为不同用户角色实现细粒度的权限类
- 实现适当的CSRF保护、CORS配置和输入清理
Performance and Scalability
性能与可扩展性
- Always use select_related and prefetch_related appropriately
- Monitor query counts and execution time in development
- Implement connection pooling for high-traffic applications
- Use Django's cache framework with Redis/Memcached
- 始终合理使用select_related和prefetch_related
- 在开发环境中监控查询数量和执行时间
- 为高流量应用实现连接池
- 使用搭配Redis/Memcached的Django缓存框架
Error Handling
错误处理
- Implement global exception handling for consistent error responses
- Use Django signals to decouple error handling
- Use appropriate HTTP status codes (400, 401, 403, 404, 422, 500)
- Implement structured logging for API monitoring and debugging
- 实现全局异常处理以确保一致的错误响应
- 使用Django信号解耦错误处理逻辑
- 使用适当的HTTP状态码(400、401、403、404、422、500)
- 实现结构化日志以用于API监控和调试