Last active
August 22, 2020 15:16
-
-
Save KimSoungRyoul/598ffea65fa3a1b521a6be10103f5465 to your computer and use it in GitHub Desktop.
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입니다.'), | |
), | |
) | |
.select_related("정방향_참조모델1","정방향_참조모델2") # EagerLoading (JOIN) | |
.filter(Q(), ~Q().. 다양한_조건절들).exclude() # 조건절 where문 반영 | |
.only() 또는 .defer() # 필요시에만 사용 | |
.prefetch_related( # EagerLoading (추가 쿼리) | |
Prefetch(to_attr="역방향_참조모델1", queryset= BModel.objects.select_related("b_model의 정방향참조모델1").filter(is_deleted=False)), | |
Prefetch(to_attr="역방향_참조모델2", queryset= CModel.objects.all()), | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment