ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|---|
|
デフォルトのテーブル名規則
# プロジェクト構造
myproject/
├── manage.py
└── apps/
└── users/ # アプリ名: "users"
├── apps.py # name = "users"
└── models.py # Profile モデル
# models.py
class Profile(models.Model):
name = models.CharField(max_length=100)
# 生成される DB テーブル名:
# → "users_profile" (アプリ名_モデル名)
命名規則の詳細
| アプリ名 | モデル名 | テーブル名 |
|---|---|---|
| users | Profile | users_profile |
| users | UserSettings | users_usersettings (小文字化、_ なし) |
| blog | BlogPost | blog_blogpost |
| shop | Order | shop_order |
| auth (Django 標準) | User | auth_user |
注意: CamelCase は小文字化されるが、単語間に _ は入らない:
class UserSettings(models.Model):
...
# → users_usersettings (× users_user_settings)
カスタムテーブル名の指定
class Profile(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = "custom_profile" # ← 任意のテーブル名
# → "custom_profile" テーブルが作成される
既存 DB との連携
# 既存の "user_profiles" テーブルを使いたい
class UserProfile(models.Model):
name = models.CharField(max_length=100, db_column="user_name") # カラム名指定
email = models.EmailField(db_column="email_address")
class Meta:
db_table = "user_profiles"
managed = False # ← Django で migrate しない(テーブル既存)
app_label の指定
# アプリ "shop" 内で他アプリ "blog" のテーブル名を使いたい
# 通常は無理だが、app_label で明示可能
# apps/shop/models.py
class Article(models.Model):
title = models.CharField(max_length=100)
class Meta:
app_label = "blog"
db_table = "blog_article" # blog_article として作成
# 注: 通常はこれを避け、proper な location にモデルを置く
Django 標準アプリのテーブル名
| アプリ | モデル | テーブル名 |
|---|---|---|
| auth | User | auth_user |
| auth | Group | auth_group |
| auth | Permission | auth_permission |
| contenttypes | ContentType | django_content_type |
| sessions | Session | django_session |
| admin | LogEntry | django_admin_log |
マイグレーション後にテーブル名を変更
# 既に "users_profile" として作成済み
# "custom_profile" に変更したい
# 1. モデルに db_table を追加
class Profile(models.Model):
...
class Meta:
db_table = "custom_profile"
# 2. makemigrations
$ python manage.py makemigrations
Migrations for 'users':
users/migrations/0002_alter_profile_options.py
- Change Meta options on profile
# 3. migrate
$ python manage.py migrate
# → RENAME TABLE users_profile TO custom_profile が実行される
現在のテーブル名を確認
# Python シェルから
$ python manage.py shell
>>> from users.models import Profile
>>> Profile._meta.db_table
'users_profile'
# SQL クエリで
>>> from django.db import connection
>>> connection.introspection.table_names()
['auth_group', 'auth_user', 'users_profile', ...]
# 個別モデルの SQL を見る
>>> from django.db import connection
>>> print(connection.queries[-1]) # 直前のクエリ
# manage.py sqlmigrate でマイグレーション SQL を表示
$ python manage.py sqlmigrate users 0001
CREATE TABLE "users_profile" (...);
命名衝突対策
異なるアプリで同名モデルがあると、テーブル名で区別:
# blog/models.py
class Post(models.Model):
title = models.CharField(max_length=100)
# → blog_post
# news/models.py
class Post(models.Model):
title = models.CharField(max_length=100)
# → news_post
# 衝突しない(アプリ名で prefix される)
# ForeignKey で参照する場合は "app.Model" 形式
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE)
注意点
- 名前変更には migrate 必須:
db_table変更 → makemigrations → migrate - 本番でのリネームは慎重に: 大規模テーブルは ALTER TABLE が長時間ロック
- managed=False: 既存 DB の場合、Django の migrate 対象から外す
- 命名規則の混在: snake_case と CamelCase が混ざらないように統一
- db_column 指定: カラム名も同様にカスタマイズ可能
関連記事
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- Model の定義方法
- マイグレーションファイルの作成
- テーブル定義の確認
- テーブルの作成
- テーブル名 = アプリケーション名 + モデル名の設定変更
- モデルの中身を確認
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- 【django】ログイン 認証機能 2026-09-08 03:22:09
- MySQL ERROR 1063 Incorrect column specifier for column|原因と直し方 2026-09-08 03:11:09
- 【PHPエラー】Object of class stdClass could not be converted to string 2026-09-07 07:46:04
- Julia Genie ローカル開発サーバ起動完全ガイド 2026-09-07 07:46:04
- Linux whichコマンドの使い方|絶対パス取得とtype・command -vの違い 2026-09-07 07:45:13
- Javaの定数 2026-09-07 07:45:13
- Laravel 記事一覧55件|環境構築・ルーティング・DB・エラー対処 2026-09-07 07:45:13
- NetBeansでTomcatを起動する手順|サーバ登録・ポート・トラブル対処 2026-09-07 07:45:13
- Spring Framework 記事一覧31件|DI・設定・例外の対処 2026-09-07 07:45:13
- JPEG(.jpg/.jpeg)画像形式の完全ガイド — 仕様・マジックナンバー・他形式との比較・EXIF 2026-09-07 07:45:13
- hostnameコマンドの使い方|Windows・Linux・macOSでホスト名確認 2026-09-07 07:45:13
- SQLとは|DDL・DMLとリレーショナルデータベース操作の基本 2026-09-07 07:45:13
- Oracle DROP USERでユーザー削除|CASCADE・接続中ユーザーの対処 2026-09-07 07:45:13
- Laravelルートグループ|prefix・middleware・nameで一括管理 2026-09-07 07:45:13
- Linuxで複数ファイルの文字列を一括置換|find・sed・xargsの安全手順 2026-09-07 07:45:13
コメントを削除してもよろしいでしょうか?