0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

0から始めるDjango開発(Part2) ユーティリティコマンドとは??

Posted at

ユーティリティコマンドについて

Djangoにはdjango-adminとmanage.pyという二種類のユーティリティコマンドがある。
前者はプロジェクト作成時しか使わないのに対し、後者はDjangoアプリケーションの作成だけでなく、様々な場面で使う。

django-adminコマンド

  • Djangoプロジェクトの作成
% django-admin startproject <プロジェクト名>

manage.pyコマンド

  • Djangoアプリケーションの自動作成
% python manage.py startapp <アプリケーション名>
  • runserverの起動
% python manage.py runserver

マイグレーション:
Djangoにおけるmigrationは、モデルの定義をDBスキーマ(テーブルやカラムの構造)に反映させるための仕組みで、簡単に言うと、DjangoモデルとDBの状態を同期させるために使う。

  • マイグレーションファイルの作成
% python manage.py makemigrations
  • マイグレーションの実行
% python manage.py migrate

マイグレーションのロールバック:
Djangoのmigrationのrollbackは、DBのスキーマ(構造)を過去の状態に戻す作業を指す。
これにより、誤った変更や不要になった変更を元に戻すことができる。

  • マイグレーション状況の確認
% python manage.py showmigrations <アプリケーション名>
app
[X] 0001_initial
[X] 0002_students
[ ] 0003_classes
[ ] 0004_test
  • マイグレーションのロールバック実行(0001_initial適用直後までもどす)
% python manage.py migrate <アプリケーション名> 0001_initial
  • マイグレーション状況の確認(ロールバックできているか確認)
% python manage.py showmigrations <アプリケーション名>
app
[X] 0001_initial
[ ] 0002_students
[ ] 0003_classes
[ ] 0004_test

スーパーユーザーの作成

サイト管理者の作成コマンド

% python manage.py createsuperuser
username: admin
mailadress: admin@example.com
Password:
Password (again):
Superuser created successfully.

テストコードの実施

% python manage.py test

全てのコマンドの確認

以下のコマンドを実行することで、利用可能なすべてのコマンドを一覧表示できる

% python manage.py help
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?