Django 장고 쿼리셋 이용방법

POOHYA·2022년 1월 24일
0

Django

목록 보기
7/7

select

SQL
select * from movie;
queryset
movie.objects.all()

where

SQL
select * from movie where id=1;
queryset
movie.objects.filter(id=1)

DESC, ASC

SQL
SELECT * FROM movie order by id DESC;
queryset
movie.objects.order_by('-id')
SQL
SELECT * FROM movie order by id ASC;
queryset
movie.objects.order_by('id')

limit

SQL
SELECT * FROM movie LIMIT 5;
queryset
movie.objects.all()[:5]

inner join

genres 와 moviegenres가 참조관계일때, 왜래키를 기준으로 select_related가 연결해줌
SQL
select genres.name from genres and moviegenres where id = 123
queryset
movie.objects.filter(id=123).select_related('moviegenres').values('moviegenres__name')

값 비교 연산 (gte, lte, gt, lt)

__gte: 크거나 같다

__gt : 크다

__lt : 작다

__lte : 작거다 같다

SQL
select * from movie where id >= 123;
queryset
movie.objects.filter(id__gte = 123).all()

날짜

__year : 해당 년도

__month : 해당 월

__day 해당 일

null값 검색

__isnull : 해당 열의 값이 null인 자료 검색

keyword

__contains : 해당 키워드가 포함된 열 조회

__icontains : 해당 키워드가 포함되지 않은 열 조회

정확하게 일치하는

exact

특정 키워드로 시작하거나 끝나는 문자열 조회

startswith : 특정 키워드로 시작

endswith : 특정 키워드로 끝

between

__range :

anotate

@@@추가중

쿼리셋짜며 팀원들이랑 SQL문을 입력하면 자동으로 queryset으로 변형해주는 프로그램이나 사이트가 있으면좋겠다고 생각했다..

참고 url
https://ieeexplore.ieee.org/xpl/conhome/1000147/all-proceedings
https://django-orm-cookbook-ko.readthedocs.io/en/latest/index.html#
https://brownbears.tistory.com/63
https://newbiecs.tistory.com/268

profile
김효주

0개의 댓글