본문 바로가기
개발일지/Django

Django - models 명령문 (filter, exclude, order_by, __lte)

by 개발에정착하고싶다 2023. 1. 9.

Django - models 명령문

 

#1 filter

 

여기서 __를 두번해주는 이유는 “쿼리셋”으로부터 끌어오는 명령이기때문에

이렇게 해주는 것이고

 

icontains는 대소문자를 가리지 않고 검색해준다는 의미다.

Post.objects.all().filter(message__icontains='123')

 

filter의 반대는 exclude이다.


#2 order_by

 

정렬 기준이다.

Post.objects.all().order_by(‘조건’)

이런식으로 사용한다 보통은


#3 __lte

 

lte시리즈는 몇가지가 있는데

 

 

1)lte(less then equal) 작거나 같다

 

2)lt(less then) 작다

 

3)gte(greater then equal) 크거나 같다

 

4)gt(greater then) 크다

 

예시 코드로는

# Post클래스 모델의 전체 데이터를 qs에 담아준다.

qs = Post.objects.all()

# qs중에서 id값이 2보다 작거나 큰것을 조회해준다. (1,2)

qs.get(id__lte = 2)

 

이다.


#4 set.all()

 

<model명1>.<model명1에엮여있는model명2>_set.all()

post.comment_set.all()