0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】自分向けDjango開発環境設定

0
Last updated at Posted at 2026-05-10

Django REST Framework開発環境の標準化メモ

Django REST FrameworkでAPIを作るときの開発環境セットアップ手順。

毎回手でパッケージを入れるのは面倒なので、必要なものはrequirements.txtにまとめておき、仮想環境作成後に一括インストールする。

採用する構成

用途 ツール
Web framework Django
API framework Django REST Framework
パッケージ管理 requirements.txt
仮想環境 venv
Linter / Formatter ruff
型チェック pyright
テスト pytest
commit前チェック pre-commit
コンテナ Docker / docker compose
CI GitHub Actions

ディレクトリ構成

drf-project/
  config/
    __init__.py
    settings.py
    urls.py
    asgi.py
    wsgi.py
  apps/
    __init__.py
    users/
      __init__.py
      admin.py
      apps.py
      models.py
      serializers.py
      urls.py
      views.py
      tests.py
      migrations/
        __init__.py
  tests/
    __init__.py
  .github/
    workflows/
      ci.yml
  .env.example
  .gitignore
  .pre-commit-config.yaml
  Dockerfile
  docker-compose.yml
  Makefile
  manage.py
  pyproject.toml
  requirements.txt
  README.md
パス 役割
config/ Djangoプロジェクト全体の設定
config/settings.py Django設定
config/urls.py 全体のURLルーティング
apps/ Django app置き場
apps/users/ users機能のapp例
apps/users/models.py DBモデル
apps/users/serializers.py DRF serializer
apps/users/views.py API view / ViewSet
apps/users/urls.py app単位のURLルーティング
tests/ app横断のテスト

requirements.txt

django
djangorestframework
python-dotenv
pytest
ruff
pyright
pre-commit

仮想環境の作成

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt

Djangoプロジェクト作成

django-admin startproject config .
mkdir -p apps/users tests
touch apps/__init__.py tests/__init__.py
python manage.py startapp users apps/users

apps/users/apps.pynameを修正する。

from django.apps import AppConfig


class UsersConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "apps.users"

settings.py

config/settings.pyINSTALLED_APPSに追加する。

INSTALLED_APPS = [
    # ...
    "rest_framework",
    "apps.users.apps.UsersConfig",
]

pyproject.toml

ruffpyrightpytestの設定をまとめる。

[project]
name = "drf-project"
version = "0.1.0"
description = "Django REST Framework API project"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

[tool.ruff]
line-length = 88
target-version = "py312"

[tool.ruff.lint]
select = [
    "E",
    "F",
    "I",
    "B",
    "UP",
]
ignore = []

[tool.ruff.format]
quote-style = "double"
indent-style = "space"

[tool.pyright]
pythonVersion = "3.12"
typeCheckingMode = "standard"
include = [
    "config",
    "apps",
    "tests",
]
exclude = [
    ".venv",
    "**/__pycache__",
]

[tool.pytest.ini_options]
testpaths = [
    "tests",
    "apps",
]
python_files = [
    "test_*.py",
    "tests.py",
]

pre-commit

.pre-commit-config.yamlを作成する。

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.8.4
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v5.0.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-toml
      - id: check-json

有効化する。

pre-commit install
pre-commit run --all-files

Makefile

install:
	python -m venv .venv
	. .venv/bin/activate && python -m pip install --upgrade pip
	. .venv/bin/activate && pip install -r requirements.txt

run:
	python manage.py runserver

migrate:
	python manage.py migrate

makemigrations:
	python manage.py makemigrations

lint:
	ruff check .

lint-fix:
	ruff check . --fix

format:
	ruff format .

format-check:
	ruff format --check .

typecheck:
	pyright

test:
	pytest

check:
	ruff check .
	ruff format --check .
	pyright
	pytest

.gitignore

.venv/
__pycache__/
.pytest_cache/
.ruff_cache/
.pyright/
.env
.DS_Store
db.sqlite3

.env.example

APP_ENV=local
SECRET_KEY=
DATABASE_URL=

実際の.envはGit管理しない。

Dockerfile

開発用の最小例。

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

本番ではrunserverは使わず、gunicornなどに切り替える。

docker-compose.yml

services:
  api:
    build: .
    ports:
      - "8000:8000"
    env_file:
      - .env
    volumes:
      - .:/app

GitHub Actions

.github/workflows/ci.yml

name: CI

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  check:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Lint
        run: ruff check .

      - name: Format check
        run: ruff format --check .

      - name: Type check
        run: pyright

      - name: Test
        run: pytest

よく使うコマンド

python manage.py runserver
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
ruff check .
ruff format .
pyright
pytest

運用ルール

  • 依存関係はrequirements.txtにまとめる
  • 初期構築はpip install -r requirements.txtで一括インストールする
  • formatはruff formatに統一する
  • import順はruff checkで整える
  • 型チェックはpyrightで見る
  • テストはpytestで書く
  • commit前にはpre-commitを通す
  • PRやpush時にはCIでruffpyrightpytestを通す
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?