This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# settings.py | |
# INSTALL_APPS 위에 있음 | |
SPECTACULAR_SETTINGS = { | |
# General schema metadata. Refer to spec for valid inputs | |
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#openapi-object | |
'TITLE': 'drf-spectacular API Document', | |
'DESCRIPTION': 'drf-specatular 를 사용해서 만든 API 문서입니다.', | |
# Optional: MAY contain "name", "url", "email" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# settings.py | |
INSTALLED_APPS = [ | |
# ... | |
'rest_framework', | |
'drf_spectacular', | |
# ... | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# urls.py | |
from drf_spectacular.views import SpectacularJSONAPIView | |
from drf_spectacular.views import SpectacularRedocView | |
from drf_spectacular.views import SpectacularSwaggerView | |
from drf_spectacular.views import SpectacularYAMLAPIView | |
urlpatterns = [ | |
# Open API 자체를 조회 : json, yaml, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SPECTACULAR_SETTINGS = { | |
# ... | |
"SWAGGER_UI_SETTINGS": { | |
'deepLinking': True, # API를 클릭할때 마다 SwaggerUI의 url이 변경됩니다. (특정 API url 공유시 유용하기때문에 True설정을 사용합니다) | |
'persistAuthorization': True, # True 이면 SwaggerUI상 Authorize에 입력된 정보가 새로고침을 하더라도 초기화되지 않습니다. | |
'displayOperationId': True, # True이면 API의 urlId 값을 노출합니다. 대체로 DRF api name둘과 일치하기때문에 api를 찾을때 유용합니다. | |
'filter': True, # True 이면 Swagger UI에서 'Filter by Tag' 검색이 가능합니다. | |
# Swagger UI 가 제공하는 UI 커스터마이징 옵션값들입니다. 아래 링크를 보면 어떤 커스터마이징 옵션들이 존재하는지 알수있습니다. | |
# https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SPECTACULAR_SETTINGS = { | |
# ..... | |
# drf-spectacular 라이브러리 version up이 없이도 자신의 원하는 swagger-ui의 version을 사용할수있다. | |
# swagger-ui version 정보는 여기서 확인 https://www.npmjs.com/package/swagger-ui | |
'SWAGGER_UI_DIST': '//unpkg.com/[email protected]', | |
'SWAGGER_UI_FAVICON_HREF': '//unpkg.com/[email protected]/favicon-32x32.png', | |
# .... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
queryset = ( | |
Model.objects. | |
.annotate( | |
커스텀프로퍼티1선언= F("DB컬럼명"), # sql AS 에 해당 | |
커스텀프로퍼티2선언=Case( | |
When(조건절_모델필드아무거나__isnull=False, # filter질의는 아무거나 다 가능 __gte, __in 등등... | |
then=Count('특정모델필드')), # 해당 값 기준으로 Count() 함수를 질의함 | |
default=Value(0, output_field=IntegerField( | |
help_text='해당 애트리뷰트 결과값을 django에서 무슨타입으로 받을건지 선언하는 param입니다.'), | |
), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.query import QuerySet,RawQuerySet | |
this_is_raw_queryset: RawQuerySet = Model.objects.raw("select * from model where ~~~~~") # raw()를 선언하면 RawQuerySet | |
this_is_queryset: QuerySet = Model.objects.filter(~~~~~~) # raw() 이외에는 전부 QuerySet을 반환한다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.query import RawQuerySet | |
from django.db.models import QuerySet | |
order_queryset: RawQuerySet = = ( | |
Order.objects | |
.raw(raw_query=""" | |
SELECT * | |
FROM "orm_practice_app_order" | |
INNER JOIN "orm_practice_app_user" ON ("orm_practice_app_order"."order_owner_id" = "orm_practice_app_user"."id") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test.utils import CaptureQueriesContext | |
from rest_framework.test import APIClient | |
def test_check_n_plus_1_problem(): | |
from django.db import connection | |
# Given: 주문이 2개더 추가되기전 API에서 발생하는 SQL Count (expected_num_queries) | |
with CaptureQueriesContext(connection) as expected_num_queries: | |
APIClient().get(path="/restaurants/") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from a_model; | |
select * from b_model where id in (~~~~) and is_deleted is False; | |
select * from c_model where id in (~~~~) ; |