django_extensions 모듈 활용하기

guava·2021년 11월 9일
0

django_extensions 모듈 설치하기

pipenv install django_extensions --dev

shell_plus 사용하기

  1. python manage.py shell_plus --print-sql을 통해 쉘에 접속한다.
  2. Post.objects.all()을 통해 쿼리를 수행한다.
  3. 쿼리 결과 뿐만 아니라 수행 시간과 쿼리 내용까지 출력된다.
python manage.py shell_plus --print-sql
# 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 instagram.models import Post
# 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.11 (default, Aug  8 2021, 20:23:04) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.29.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: Post.objects.all
Out[1]: <bound method BaseManager.all of <django.db.models.manager.Manager object at 0x10f994dc0>>

In [2]: Post.objects.all()
Out[2]: SELECT VERSION(), @@sql_mode, @@default_storage_engine, @@sql_auto_is_null, @@lower_case_table_names,
       CONVERT_TZ('2001-01-01 01:00:00', 'UTC', 'UTC') IS NOT NULL

Execution time: 0.003324s [Database: default]
   SET
SESSION
TRANSACTION
ISOLATION LEVEL READ COMMITTED

Execution time: 0.002954s [Database: default]
SELECT `instagram_post`.`id`,
       `instagram_post`.`message`,
       `instagram_post`.`is_publish`,
       `instagram_post`.`photo`,
       `instagram_post`.`created_at`,
       `instagram_post`.`updated_at`
  FROM `instagram_post`
 ORDER BY `instagram_post`.`id` DESC
 LIMIT 21

Execution time: 0.004925s [Database: default]
<QuerySet [<Post: testclean_message>, <Post: test>, <Post: 세번째 메시지clean_message>, <Post: 두번째 메시지>, <Post: 첫번째 메시지>]>

0개의 댓글