https://django-extensions.readthedocs.io/en/latest/
$ pip install django-extensions
1) django 프로젝트의 settings.py
INSTALLED_APPS 에 'django_extensions' 추가
INSTALLED_APPS = [
...
'django_extensions',
]
2) python manage.py shell_plus --print-sql 실행
(venv) # python manage.py shell_plus --print-sql
# Shell Plus Model Imports
from instagram.models import Post
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
# 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.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>>
>>> Post.objects.all()
SELECT "instagram_post"."id",
"instagram_post"."message",
"instagram_post"."photo",
"instagram_post"."is_public",
"instagram_post"."created_at",
"instagram_post"."updated_at"
FROM "instagram_post"
ORDER BY "instagram_post"."id" DESC
LIMIT 21
Execution time: 0.001612s [Database: default]
<QuerySet [<Post: 세번째 메세지>, <Post: 두번째 메세지>, <Post: 첫번째 메세지>]>
>>>