일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- 파이썬버전변경
- 맥북파이썬여러버전
- vscode소스제어 무한로딩
- 'message': 'authentication failed'
- 티스토리챌린지
- ncp authentication failed
- codepipeline 오류
- 파이썬 경로 수정
- 'details': 'invalid authentication information.'}}
- vscode 소스제어
- vscode무한로딩
- 네이버클라우드 authentication failed
- ncp api 응답오류
- ncp 401 authentication failed
- fcm push
- fcm 멀티푸시
- vscode시계아이콘
- codebuild cliend_error
- fcm multicast
- {'error': {'errorcode': '200'
- route53 서브도메인
- command did not exit successfully docker-compose -f docker-compose.yml build exit status 1
- 파이썬버전여러개
- 파이썬여러버전변경
- 오블완
- rds utf8mb4 변경
- 파이썬 설치경로 변경
- codebuild no matching artifact paths found
- rds character_set 변경
- rds 파라미터그룹
- Today
- Total
All that I've dreamed of
[Django] filter() 함수 AND / OR 본문
새로운 쿼리셋을 반환하는 메서드 중 get() 과 더불어 정말 많이 사용되는 메서드인 filter()
* Django 공식문서
filter()¶
filter(*args, **kwargs)¶
Returns a new QuerySet containing objects that match the given lookup parameters.
The lookup parameters (**kwargs) should be in the format described in Field lookups below. Multiple parameters are joined via AND in the underlying SQL statement.
If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects (*args).
**kwargs와 *args 두개의 파라미터를 받는데, **kwargs를 lookup 파라미터라고 하고 Field lookups에 나와있는 형태를 쓴다고 한다.
정리해 보자면 조건문 (WHERE) 의 역할이고 흔히 사용되는 __contains, __startswith,... 와 같은 것들이고 filter함수를 쓸 때 꼭 들어가야한다.
그럼 *args는..?
마지막 줄을 보니 필수적으로 들어가야 하는 건 아니고 좀 더 복잡한 쿼리를 실행하고 싶을 때 (OR와 같은..) 사용하면 된다고 한다
from django.db.models import Q
filter_set = {
"adult": "gte",
"underate": "lt",
}
age = "adult"
age_filter = {f"age__{filter_set[age]}": 20}
# age_filter = {"age__gte":20}
User.objects.filter(Q(username__startswith="KIM"), **kwargs)
#**kwargs = age__gte=20
-> 조건: (이름이 KIM 으로 시작) AND (age 가 20 이상)
* Q오브젝트 문서 참조
https://docs.djangoproject.com/en/4.1/topics/db/queries/#complex-lookups-with-q
'Django' 카테고리의 다른 글
[Django] Q객체로 filter() 사용하기 (0) | 2022.12.21 |
---|---|
[Django] MySQL 연동 (Docker 컨테이너) (0) | 2022.11.16 |
Django openpyxl로 생성한 엑셀파일 templates으로 보내기 (0) | 2022.07.04 |
Django-Firestore 연결 데이터 조회 불러오기 (0) | 2022.06.30 |
Django #1 ManyToMany / related_name (0) | 2022.04.26 |