320x100
부분적으로는 분명 아는 개념들인데, 이것들이 하나가 되었을때 비로소 힘을 발하는 구조에 대해서
이런 구조는 못본 것 같다.
거두절미하고
#1 or 조건
from django.db.models import Q
Book.objects.filter(Q(rating__lt=3)| Q(is_bestselling=True))
터미널에서 python shell을 실행한 후에
첫째줄을 실행하여 Q를 import해주고
두번째 조건문으로 조회를 하고자 했다.
이 or 조건은 rating이 3보다 작거나
베스트셀링이 True인 값들을 필터링하여
리턴해준다.
정말 python식 sql같다.
#2-1 or조건에 and 조건을 붙인 것
from django.db.models import Q
Book.objects.filter(Q(rating__lt=3)| Q(is_bestselling=True), Q(author="Caribian"))
#1의 조건에 author가 Caribian인 사람을 찾아라 라는 and 조건이다.
즉, and는 콤마와 Q를 이용하면 된다.
#2-2 그냥 표현만 달리 한 and 식
from django.db.models import Q
Book.objects.filter(Q(rating__lt=3)| Q(is_bestselling=True), author="Caribian")
그저 이렇게 Q를 빼고 봐줘도 작동한다.
300x250
'개발일지 > Python' 카테고리의 다른 글
* 매우중요 Python - 문장에 들어가는 단어중 (소문자,대문자,숫자)가 사용되었는지 확인 (1) | 2022.12.28 |
---|---|
python - 파이썬 코드 과정 시각화 출력 사이트 추천 (0) | 2022.12.15 |
Python - (filter lookup) shell 사용에 있어서 rating filter 확인 방법 (>=<비교 등호 사용) (0) | 2022.11.27 |
Python 가상환경 설정 링크 (env, venv) (0) | 2022.10.07 |
*매우매우중요 Python class5 - 파이썬 파일 import (0) | 2022.09.28 |