2011년 2월 18일 금요일

[Django] Query 명령어 (일부만) 메모

가끔 잊어먹지만 문서 보기가 귀찮으니 자주 쓰는 것만 따로 정리.
Query Set:
- get 
- get_or_create (obj, created 튜플이 리턴되니 주의) 
- exists 
- update 
- delete 
(물론 filter는 별도)

Field Look-up:
- __exact / __iexact 
- __contains / __icontains / __search 
- __in (with list) 
- __gt / __gte 
- __lt / __lte 
- __startswith / __istartswith 
- __endswith / __iendswith 
- __range 
- __year / __month / __day 
- __isnull 
(i로 시작되는 이름은 ignore case)

Raw Query with Model:
r = ModelClass.objects.raw("SQL Query String")
(Insert문은 model로 raw query가 안되니 아래 참고)

Raw Query with Cursor:
from django.db import connection, transaction

cursor = connection.cursor()
cursor.execute("SQL Query String")
transaction.commit_unless_managed()

댓글 없음 :