1️⃣ QuerySet이 반환되는 경우의 주요 method
2️⃣ QuerySet이 반환되지 않는 경우의 주요 method(다음 편에서)
3️⃣ filter()와 get()의 차이점(다음 편에서)
In : Product.objects.all() Out : <QuerySet [<Product: Product object (1)>, <Product: Product object (2)>, <Product: Product object (3)>, <Product: Product object (4)>, <Product: Product object (5)>, <Product: Product object (6)>, <Product: Product object (7)>, <Product: Product object (8)>, <Product: Product object (9)>, <Product: Product object (10)>, <Product: Product object (11)>, <Product: Product object (12)>, <Product: Product object (13)>, <Product: Product object (14)>, <Product: Product object (15)>, <Product: Product object (16)>, <Product: Product object (17)>, <Product: Product object (18)>, <Product: Product object (19)>, <Product: Product object (20)>] In : for product in Product.objects.all() print(product.english_name) Out : Nitro Vanilla Cream Nitro Cold Brew Dolce Cold Brew Vanilla Cream Cold Brew ... Orange juice
**case1** In : Product.objects.filter(category_id=2) Out : [<Product: Product object (8)>, <Product: Product object (9)>] **case2** In : Product.objects.filter(category_id=2).filter(english_name='Iced coffee') Out : <QuerySet [<Product: Product object (8)>]> **case3** In : Product.objects.filter(category_id=2).exclude(english_name='Iced coffee') Out : <QuerySet [<Product: Product object (9)>]>
In : Product.objects.filter(category_id = 2) Out : [<Product: Product object (8)>, <Product: Product object (9)>] In : Product.objects.filter(category_id = 2).values() Out : <QuerySet [{'id': 8, 'korean_name': '아이스 커피', 'english_name': 'Iced Coffee', 'description': '깔끔하고 상큼함이 특징인 시원한 아이스 커피', 'category_id': 2}, {'id': 9, 'korean_name': '오늘의 커피', 'english_name': 'Brewed Coffee', 'description': '신선하게 브루드(Brewed)되어 원두의 다양함이 살아있는 커피', 'category_id': 2}]>
In : Product.objects.filter(category_id = 2).values_list() Out : <QuerySet [(8, '아이스 커피', 'Iced Coffee', '깔끔하고 상큼함이 특징인 시원한 아이스 커피', 2), (9, '오늘의 커피', 'Brewed Coffee', '신선하게 브루드(Brewed)되어 원두의 다양함이 살아있는 커피', 2)]>
QuerySet이 반환되는 경우에 대해 주요 method에 대해 알아보았다. 각 method 마다 QuerySet이 반환되거나 안되는 경우가 있어서 사용시 주의를 해야할 것 같다. 다음 편에서 QuerySet이 반환 안되는 경우에 대해 알아볼 것 이다.