์ ๋ฌ๋ฐ์ ๋ชจ๋ธ์ ๊ฐ์ฒด๋ชฉ๋ก
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ํํฐ๋ฅผ ๊ฑธ๊ฑฐ๋ ์ ๋ ฌํ ์ ์๋ค
๐ 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()
get()
update()
delete()
save()
exists()
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()
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>))]>