LoginSignup
0
2

Djangoでshell_plusコマンドを実行する手順

Posted at

はじめに

Djangoで開発中にpython manage.py shellなどでインタープリタを開くことが出来ますが、さらに拡張されたshell_plusというコマンドを最近知ったので情報をまとめておきます。

shell_plusでできること

プロジェクト内の各モデルやDjangoでよく利用される関数をimportした状態でshellを開始することができます。
実際にshell_plusを実行するとDjango内のモデルや関数のインポートが自動で実行されていることがターミナル上でも確認できます。

(practical-django) akis@ipf:/mnt/c/Users/saki0/dev/practical-django/djangosnippets$ python manage.py shell_plus
# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
from snippets.models import Snippet
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When
from django.utils import timezone
from django.urls import reverse
from django.db.models import Exists, OuterRef, Subquery
Python 3.9.17 (main, Nov 19 2023, 20:12:46) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

shell_plusの導入方法

django-extensionsのインストール

pip install django-extensions

私はpipenvを使っていたので、インストールする場合はこちらのコマンドを打ちました。

pipenv install --dev django-extensions

INSTALLED_APPSにdjango_extensionsを追加
settings.pyINATALLED_APPSdjango_extensionsを追加します。

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions', ここに追記
    'snippets'
]

起動
下記コマンドを実行します。

python manage.py shell_plus

参考資料

実践Django

image.png

0
2
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
2