[PL]/Python
[Django] Django ORM
객과 함께.
2021. 9. 13. 17:41
일반 SQL문에 WHERE절에 대한 DJANGO ORM 표현방법
───────────────────────────────────────────
조회 조건 설명
─────────────┬─────────────────────────────
__contains │ 지정한 문자열을 포함하는 데이터 조회
│ Post.objects.filter(title__contains=’test’)
─────────────┼─────────────────────────────
__icontains │ 지정한 문자열의 대소문자 구분없이 포함하는 데이터 조회
│ Post.objects.filter(title__icontains=’this’)
─────────────┼─────────────────────────────
__lt │ 값이 작은 경우(lt: less than)
│ Post.objects.filter(published_at__lt=timezone.now())
─────────────┼─────────────────────────────
__lte │ 값이 작거나 같은 경우(lte: less than or equal)
│ Post.objects.filter(published_at__lt=timezone.now())
─────────────┼─────────────────────────────
__gt │ 값이 큰 경우(gt: greater than)
│ Post.objects.filter(published_at__gt=timezone.now())
─────────────┼─────────────────────────────
__gte │ 값이 크거나 같은 경우(gt: greater than or equal)
│ Post.objects.filter(published_at__gte=timezone.now())
─────────────┼─────────────────────────────
__in │ 주어진 리스트에 포함되는 데이터 조회
│ Post.objects.filter(id__in=[1, 2, 3])
─────────────┼─────────────────────────────
__year │ 해당 년도 조회
│ Post.objects.filter(created_at__year=’2019’)
─────────────┼─────────────────────────────
__year │ 해당 월로 조회
│ Post.objects.filter(created_at__month=’5’)
─────────────┼─────────────────────────────
__day │ 해당 일로 조회
│ Post.objects.filter(created_at__day=’21’)
─────────────┼─────────────────────────────
__isnull │ 해당 열이 null인 데이터 조회
│ Post.objects.filter(published_at__isnull=True)
─────────────┼─────────────────────────────
__startswith │ 해당 문자열로 시작하는 데이터 조회
│ Post.objects.filter(title__startswith=’This’)
─────────────┼─────────────────────────────
__istartswith │ 대소문자를 가리지 않고 해당 문자열로 시작하는 데이터 조회
│ Post.objects.filter(title__istartswith=’this’)
─────────────┼─────────────────────────────
__endswith │ 해당 문자열로 끝나는 데이터 조회
│ Post.objects.filter(title__endswith=’title’)
─────────────┼─────────────────────────────
__iendswith │ 대소문자를 가리지 않고 해당 문자열로 끝나는 데이터 조회
│ Post.objects.filter(title__iendswith=’title’)
─────────────┼─────────────────────────────
__range │ 범위를 지정하여 조회(sql의 between)
│ Post.objects.filter(id__range=(1, 10)
─────────────┴─────────────────────────────
2). JOIN 관련
brownbears.tistory.com/101
3). fields 이름 변경 방법
brownbears.tistory.com/100?category=168282
4). FilterSet Guide
brownbears.tistory.com/98?category=168282
5). Django Rest Framework
brownbears.tistory.com/97?category=168282