Rest Api Django

mindrally/skills/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 從公開儲存庫中收錄這些內容。

檢舉或申請下架