rest-api-django
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseREST API Development with Django
使用Django开发REST API
You are an expert in Django REST Framework for building scalable APIs.
您是使用Django REST Framework构建可扩展API的专家。
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
- 尽可能使用Django的内置功能
- 遵循PEP 8规范,优先保证代码可读性
- 使用小写下划线命名的描述性名称
- 利用Django应用来构建项目结构,提升可复用性
- 在所有设计决策中考虑可扩展性
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 tasksapp_name/
migrations/ # 数据库迁移文件
admin.py # 后台管理配置
models.py # 数据模型
managers.py # 自定义模型管理器
signals.py # Django信号
tasks.py # Celery任务API Structure
API结构
api/
v1/
urls.py # URL routing
serializers.py # DRF serializers
views.py # API views
permissions.py # Custom permissions
filters.py # Query filtersapi/
v1/
urls.py # URL路由
serializers.py # DRF序列化器
views.py # API视图
permissions.py # 自定义权限
filters.py # 查询过滤器Development Guidelines
开发指南
Views and API Design
视图与API设计
- 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
- 使用DRF的APIViews基于类的视图
- 遵循RESTful原则设计端点
- 将业务逻辑放在模型中,而非视图
- 保持响应格式一致
Models and Database
模型与数据库
- Leverage Django ORM for all database operations
- Use and
select_related()to prevent N+1 queriesprefetch_related() - Apply proper indexing on frequently queried fields
- Use for critical operations
transaction.atomic()
- 所有数据库操作都使用Django ORM
- 使用和
select_related()避免N+1查询问题prefetch_related() - 在频繁查询的字段上添加合适的索引
- 对关键操作使用
transaction.atomic()
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
- 所有数据转换都使用DRF序列化器
- 为复杂验证实现自定义验证器
- 正确处理嵌套关系
- 保持序列化器的专注性和可组合性
Authentication and Authorization
认证与授权
- Use for JWT authentication
djangorestframework_simplejwt - Implement granular permissions per endpoint
- Ensure CSRF protection for session-based auth
- Apply principle of least privilege
- 使用实现JWT认证
djangorestframework_simplejwt - 为每个端点实现细粒度的权限控制
- 确保基于会话的认证具备CSRF保护
- 应用最小权限原则
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
- 通过预加载避免N+1查询
- 实现数据库连接池
- 使用Redis或Memcached进行缓存
- 对列表端点应用标准化分页
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
python
{
"success": False,
"message": "Validation failed",
"errors": {
"field_name": ["Error message"]
},
"error_code": "VALIDATION_ERROR"
}- 使用合适的HTTP状态码
- 返回一致的错误响应结构
- 应用结构化日志便于调试
- 绝不向客户端暴露内部错误