Django QuerySet API

๊น€ํ•œ์ค€ยท2021๋…„ 6์›” 28์ผ
0

๐Ÿš€ QuerySet

์ „๋‹ฌ๋ฐ›์€ ๋ชจ๋ธ์˜ ๊ฐ์ฒด๋ชฉ๋ก
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ํ•„ํ„ฐ๋ฅผ ๊ฑธ๊ฑฐ๋‚˜ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค

๐Ÿ“ Model Method ์ข…๋ฅ˜

all()
filter()
exclude()	
values()
values_list()
get()	
create()
count()		
exists()		
update()	
delete()      
first()	
last()

โ€ฆ

๐Ÿ“ QuesrySet์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ

<QuerySet [<Category: Category object(1)>, <Category: Category object (2)>]>

๐Ÿ“ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ

<Category: Category object (1)>, 9, Trueโ€ฆ

๐Ÿ”ฅ Methods That Donโ€™t Return QuerySets

Create()

  • Table์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ ํ•ด์ฃผ๋Š” method. ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜

get()

  • ์ง€์ •๋œ ์กฐํšŒ ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜

update()

  • ์ง€์ •๋œ ํ•„๋“œ์— ๋Œ€ํ•ด ์—…๋ฐ์ดํŠธ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ผ์น˜ํ•˜๋Š” ํ–‰ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

delete()

  • QuerySet์˜ ๋ชจ๋“ ํ–‰์— ๋Œ€ํ•ด SQL ์‚ญ์ œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‚ญ์ œ ๋œ ๊ฐœ์ฒด์ˆ˜์™€ ๊ฐœ์ฒด ์œ ํ˜•๋ณ„ ์‚ญ์ œํšŸ์ˆ˜๊ฐ€ ์žˆ๋Š” dictionary๋ฅผ ๋ฐ˜ํ™˜

save()

  • Insert ์™€ Update๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” method
    ๋‹จ์ผ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ

exists()

  • filter์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ
    filter ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ์กฐํšŒ
    True or False

count()
get_or_create()
bulk_create()
first()
last()
aggregate()

๐Ÿ”ฅ Methods That Return QuesrySets

all()

  • ํ•œ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด all()method
    QuerySet์„ ๋ฐ˜ํ™˜ ๊ฐ๊ฐ ์ธ์Šคํ„ด์Šค ํฌํ•จ

    In  : Category.objects.all()
    Out : <QuerySet [<Category: Category object (2)>, <Category: Category object (3)>, <Category: Category object (4)>, <Category: Category object (5)>, <Category: Category object (6)>, <Category: Category object (7)>]>
    
    In  : for category in Category.objects.all()
    				print(category.name)
    
    #์•„๋ž˜์™€ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค๋“ค์ด ๋‹ด๊ฒจ ์žˆ๋Š” QuerySet์ด ๋ฐ˜ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“  ์†์„ฑ์— ์ ‘๊ทผํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    Out : ํ‹ฐ
      ๋ธŒ๋ฃจ๋“œ์ปค๋ฆฌ
      ๋ธŒ๋ฃจ๋“œ์ปคํ”ผ
      ์ฝœ๋“œ๋ธŒ๋ฃจ
      ์ฝœ๋“œ๋ธŒ๋ฃจ

filter()&exclude()

  • ํ…Œ์ด๋ธ”์˜ ํŠน์ • ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ method
    filter(**kwargs): ํ‚ค์›Œ๋“œ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ lookup ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋“ค์˜ QuerySet์„ ๋ฐ˜ํ™˜

values()

  • iterable๋กœ ์‚ฌ์šฉ๋  ๋•Œ ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ dictionary์„ ํฌํ•จํ•˜๋Š” QuerySet์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    In  : Category.objects.filter(name='๋ธŒ๋ฃจ๋“œ์ปคํ”ผ').values()
    Out : <QuerySet [{'id': 3, 'name': '๋ธŒ๋ฃจ๋“œ์ปคํ”ผ', 'created_at': datetime.datetime(2020, 9, 8, 5, 43, 30, 4068, tzinfo=<UTC>), 'updated_at': datetime.datetime(2020, 9, 8, 5, 43, 30, 21801, tzinfo=<UTC>)}, {'id': 4, 'name': '๋ธŒ๋ฃจ๋“œ์ปคํ”ผ', 'created_at': datetime.datetime(2020, 9, 8, 5, 43, 30, 4068, tzinfo=<UTC>), 'updated_at': datetime.datetime(2020, 9, 8, 5, 43, 30, 21801, tzinfo=<UTC>)}]>

values_list()

  • values_list() method๋Š” dictionary๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋Œ€์‹  ๋ฐ˜๋ณต ๋  ๋•Œ ํŠœํ”Œ์„ ๋ฐ˜ํ™˜

    	In  : Category.objects.filter(name='๋ธŒ๋ฃจ๋“œ์ปคํ”ผ').values_list()
    	Out : <QuerySet [(3, '๋ธŒ๋ฃจ๋“œ์ปคํ”ผ', datetime.datetime(2020, 9, 8, 5, 43, 30, 4068, tzinfo=<UTC>), datetime.datetime(2020, 9, 8, 5, 43, 30, 21801, tzinfo=<UTC>)), (4, '๋ธŒ๋ฃจ๋“œ์ปคํ”ผ', datetime.datetime(2020, 9, 8, 5, 43, 30, 4068, tzinfo=<UTC>), datetime.datetime(2020, 9, 8, 5, 43, 30, 21801, tzinfo=<UTC>))]>

0๊ฐœ์˜ ๋Œ“๊ธ€