Django REST Framework 项目结构 #

一、基础项目结构 #

1.1 单应用结构(小型项目) #

适合小型项目或学习阶段:

text
myproject/
├── manage.py
├── requirements.txt
├── .env
├── .gitignore
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
└── api/
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── serializers.py
    ├── views.py
    ├── urls.py
    ├── tests.py
    └── migrations/
        └── __init__.py

1.2 多应用结构(中型项目) #

适合中型项目,按功能模块划分:

text
myproject/
├── manage.py
├── requirements/
│   ├── base.txt
│   ├── development.txt
│   └── production.txt
├── config/
│   ├── __init__.py
│   ├── settings/
│   │   ├── __init__.py
│   │   ├── base.py
│   │   ├── development.py
│   │   └── production.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
├── apps/
│   ├── __init__.py
│   ├── users/
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── serializers.py
│   │   ├── views.py
│   │   ├── urls.py
│   │   └── tests/
│   │       ├── __init__.py
│   │       ├── test_models.py
│   │       └── test_views.py
│   ├── articles/
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── serializers.py
│   │   ├── views.py
│   │   ├── urls.py
│   │   └── tests/
│   └── comments/
│       └── ...
├── core/
│   ├── __init__.py
│   ├── exceptions.py
│   ├── middleware.py
│   ├── pagination.py
│   ├── permissions.py
│   └── utils.py
├── static/
│   └── ...
└── media/
    └── ...

1.3 企业级结构(大型项目) #

适合大型企业级项目:

text
myproject/
├── manage.py
├── docker-compose.yml
├── Dockerfile
├── requirements/
│   ├── base.txt
│   ├── development.txt
│   ├── production.txt
│   └── testing.txt
├── config/
│   ├── __init__.py
│   ├── settings/
│   │   ├── __init__.py
│   │   ├── base.py
│   │   ├── development.py
│   │   ├── production.py
│   │   └── testing.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py
├── apps/
│   ├── __init__.py
│   ├── users/
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── models/
│   │   │   ├── __init__.py
│   │   │   ├── user.py
│   │   │   └── profile.py
│   │   ├── serializers/
│   │   │   ├── __init__.py
│   │   │   ├── user.py
│   │   │   └── profile.py
│   │   ├── views/
│   │   │   ├── __init__.py
│   │   │   ├── user.py
│   │   │   └── profile.py
│   │   ├── urls.py
│   │   ├── tests/
│   │   │   ├── __init__.py
│   │   │   ├── factories.py
│   │   │   ├── test_models.py
│   │   │   ├── test_views.py
│   │   │   └── test_serializers.py
│   │   ├── services/
│   │   │   ├── __init__.py
│   │   │   └── user_service.py
│   │   └── migrations/
│   ├── articles/
│   └── ...
├── core/
│   ├── __init__.py
│   ├── exceptions.py
│   ├── middleware.py
│   ├── pagination.py
│   ├── permissions.py
│   ├── throttling.py
│   ├── utils.py
│   └── validators.py
├── docs/
│   └── api/
│       └── openapi.yaml
├── scripts/
│   ├── deploy.sh
│   └── backup.sh
├── static/
│   └── ...
├── media/
│   └── ...
└── templates/
    └── ...

二、应用内部结构 #

2.1 标准应用结构 #

text
articles/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── serializers.py
├── views.py
├── urls.py
├── tests.py
└── migrations/

2.2 扩展应用结构 #

text
articles/
├── __init__.py
├── admin.py
├── apps.py
├── models/
│   ├── __init__.py
│   ├── article.py
│   ├── category.py
│   └── tag.py
├── serializers/
│   ├── __init__.py
│   ├── article.py
│   ├── category.py
│   └── tag.py
├── views/
│   ├── __init__.py
│   ├── article.py
│   ├── category.py
│   └── tag.py
├── urls.py
├── tests/
│   ├── __init__.py
│   ├── factories.py
│   ├── test_models.py
│   ├── test_views.py
│   └── test_serializers.py
├── services/
│   ├── __init__.py
│   └── article_service.py
├── migrations/
└── management/
    └── commands/
        └── custom_command.py

三、配置管理 #

3.1 分离配置文件 #

创建 config/settings/ 目录:

text
config/settings/
├── __init__.py
├── base.py        # 基础配置
├── development.py # 开发环境
├── production.py  # 生产环境
└── testing.py     # 测试环境

3.2 基础配置 #

config/settings/base.py

python
import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent.parent

SECRET_KEY = os.getenv('SECRET_KEY', 'dev-secret-key')

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'django_filters',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'config.urls'

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ],
    'DEFAULT_PARSER_CLASSES': [
        'rest_framework.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser',
    ],
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,
}

3.3 开发环境配置 #

config/settings/development.py

python
from .base import *

DEBUG = True

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

INSTALLED_APPS += [
    'debug_toolbar',
]

MIDDLEWARE.insert(0, 'debug_toolbar.middleware.DebugToolbarMiddleware')

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

INTERNAL_IPS = ['127.0.0.1']

REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES'] = [
    'rest_framework.permissions.AllowAny',
]

3.4 生产环境配置 #

config/settings/production.py

python
import os
from .base import
最后更新:2026-03-28