본문으로 바로가기

django 쿼리셋

category Python 2019. 10. 23. 12:27

<CASE 1>
try:
    costData = InCost.objects.get(sg_sdate=chk_sdate)
    print('데이터 있음')
except InCost.DoesNotExist:
    print('데이터 없음')
    pass
except InCost.MultipleObjectsReturned:
    print('여러개 Item')
    pass


<CASE 2>
costData = InCost.objects.filter(sg_sdate=chk_sdate)
if costData:
   print('데이터 있음')
else:
   print('데이터 없음')
   
   
   
<CASE 3>   
people = Person.objects.raw('SELECT *, age(birth_date) AS age FROM myapp_person')
for p in people:
	print("%s is %s." % (p.first_name, p.age))
    
John is 37.
Jane is 42.
...
   
   

 

참고 

https://docs.djangoproject.com/en/2.2/topics/db/sql/