[RDBMS] QuerySet๐Ÿ’ก

Inah-_-ยท2021๋…„ 3์›” 3์ผ
0

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

๋ชฉ๋ก ๋ณด๊ธฐ
4/13
post-thumbnail

Django QuerySet API

Database์—์„œ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ์ฒด๋“ค์˜ ๋ชจ์Œ(list)

Python์œผ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๊ฐ€ SQL๋กœ mapping๋˜์–ด <QuerySet>์ด๋ผ๋Š” ์ž๋ฃŒ ํ˜•ํƒœ๋กœ ๊ฐ’์ด ๋„˜์–ด์˜ค๊ฒŒ ๋œ๋‹ค.

Model Method

Methods That Don't Return QuerySets

create()

table์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€(insert) ํ•ด์ฃผ๋Š” method, ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค ๋ฐ˜ํ™˜
<class๋ช…>.objects.create()

Category.objects.create(name='์ฝœ๋“œ๋ธŒ๋ฃจ')
<Category: Category object (1)>

#category ๋ณ€์ˆ˜์— ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ์ƒ์„ฑ๋œ data๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
#์ธ์Šคํ„ด์Šค๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ category.name์œผ๋กœ class ์•ˆ์— ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
category = Category.objects.create(name='์ฝœ๋“œ๋ธŒ๋ฃจ')
category.name
'์ฝœ๋“œ๋ธŒ๋ฃจ'

get()

์ง€์ •๋œ ์กฐํšŒ ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š” ์ธ์Šคํ„ด์Šค ๋ฐ˜ํ™˜, ์ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ํ•„๋“œ ์กฐํšŒ์— ์„ค๋ช… ๋œ ํ˜•์‹์ด์–ด์•ผ ํ•œ๋‹ค.

Category.objects.get(id=1)
<Category: Category object (1)>

update()

์ง€์ •ํ•œ ํ•„๋“œ์˜ ์ฟผ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธ
name์˜ '์ฝœ๋“œ๋ธŒ๋ฃจ' ๊ฐ’์„ ๊ฐ€์ง„ ๋ชจ๋“  row๊ฐ€ update๋œ๋‹ค.

Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ').update(name='๋Œ์ฒด ์ฝœ๋“œ๋ธŒ๋ฃจ')
2 # ์ด ์—…๋ฐ์ดํŠธ ๋˜์–ด ๋ฐ˜ํ™˜๋œ row์˜ ๊ฐœ์ˆ˜

delete()

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

Category.objects.filter(name='๋Œ์ฒด ์ฝœ๋“œ๋ธŒ๋ฃจ').delete()
(2, {'products.Category':1}) # (์‚ญ์ œ ๋œ ๊ฐœ์ฒด์ˆ˜, {์‚ญ์ œ๋œ ๊ฐœ์ฒด ์œ ํ˜•:})

save()

insert ๋˜๋Š” update๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
๋‹จ์ผ ๊ฐ์ฒด์— ๋Œ€ํ•ด ์—…๋ฐ์ดํŠธํ•  ๋•Œ ๋งŽ์ด ์“ฐ์ธ๋‹ค.

category = Category.objects.get(id=2)
<Category: Category object (2)>

category.name
'๋ธŒ๋ฃจ๋“œ์ปคํ”ผ'

category.name = 'new name'
category.save()

category.name
'new name'

exists()

filter()์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ,
filter ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”์ง€ ์กฐํšŒํ•œ๋‹ค.
์กด์žฌ์‹œ True, ์กด์žฌํ•˜์ง€ ์•Š์„์‹œ False๋ฅผ ๋ฐ˜ํ™˜

Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ').exists()
True

๊ทธ ๋ฐ–์— QuerySets์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๋ฉ”์†Œ๋“œ

get_or_create()
bulk_create()
count()
first()
last()
aggreate()




QuerySet ๋ฐ˜ํ™˜

<QuerySet [<"Class๋ช…": "Class๋ช…" object (1)>, <"Class๋ช…": "Class๋ช…"object (2)>]>

Methods That Return QuerySets

all()

ํ•œ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
๊ทธ ๊ฒฐ๊ณผ๋กœ ๊ฐ๊ฐ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ํฌํ•จ๋œ QuerySet์„ ๋ฐ˜ํ™˜

Category.objects.all()
<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)>]>

for category in Category.objects.all()
					print(category.name)

#์•„๋ž˜์™€ ๊ฐ™์ด ์ธ์Šคํ„ด์Šค๋“ค์ด ๋‹ด๊ฒจ ์žˆ๋Š” QuerySet์ด ๋ฐ˜ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“  ์†์„ฑ์— ์ ‘๊ทผํ•ด์„œ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ
#Output : ํ‹ฐ
      ๋ธŒ๋ฃจ๋“œ์ปค๋ฆฌ
      ๋ธŒ๋ฃจ๋“œ์ปคํ”ผ
      ์ฝœ๋“œ๋ธŒ๋ฃจ
      ์ฝœ๋“œ๋ธŒ๋ฃจ

filter() & exclude()

filter keyword ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋“ค์˜ QuerySet์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

case1
Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ')
[<Category: Category object (3)>, <Category: Category object (4)>]

case2
Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ').filter(id=3)
[<Category: Category object (3)>]

case3
Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ').exclude(id=3)
[<Category: Category object (4)>]

valuse()

(iterable = ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด)๋กœ ์‚ฌ์šฉ๋  ๋•Œ, model ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹Œ
dictionary๋ฅผ ํฌํ•จํ•˜๋Š” QuerySet ๋ฐ˜ํ™˜

Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ')
[<Category: Category object (3)>, <Category: Category object (4)>]

Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ').values()
<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๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋Œ€์‹  ๋ฐ˜๋ณต ๋  ๋•Œ ํŠœํ”Œ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๋ฉด values ()์™€ ๊ฐ™๋‹ค.

Category.objects.filter(name='์ฝœ๋“œ๋ธŒ๋ฃจ').values_list()
<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>))]>

Reference

Queryset_API_Reference

profile
Backend Developer

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