<QuerySet [<Category: Category object (1)>, <Category: Category object (2)>]>
1)
In: Category.objects.all() Out : <QuerySet [<Category: Category object (0)>, <Category: Category object (1)>, <Category: Category object (2)>]>
2)
In : for category in Category.objects.all() print(category.name) Out : 티 브루드커피 브루드커피 콜드브루 콜드브루
filter(**kwargs)
키워드 인자로 주어진 lookup 조건에 일치하는 레코드들의 QuerySet 반환.# case1 In : Category.objects.filter(name='브루드커피') Out : [<Category: Category object (3)>, <Category: Category object (4)>] # case2 In : Category.objects.filter(name='브루드커피').filter(id=3) Out : [<Category: Category object (3)>] # case3 In : Category.objects.filter(name='브루드커피').exclude(id=3) Out : [<Category: Category object (4)>]
In : Category.objects.filter(name='브루드커피') Out : [<Category: Category object (3)>, <Category: Category object (4)>] In : Category.objects.filter(name='브루드커피').values() Out : <QuerySet [{'id': 3, 'name': '브루드커피'}, {'id': 4, 'name': '브루드커피'}]>
In : Category.objects.filter(name='브루드커피').values_list() Out : <QuerySet [(3, '브루드커피'), (4, '브루드커피')]>
<Category: Category object (1)> , 9 , True ..
In : Category.objects.create(name='콜드브루') Out : <Category: Category object (1)> #category 변수에 반환된 값을 저장하고, 생성된 data를 사용할 수 있다. #인스턴스로 반환되므로 category.name으로 class 안에 변수에 접근할 수 있다. In : category = Category.objects.create(name='콜드브루') In : category.name Out : '콜드브루' 참고) save method : INSERT 또는 UPDATE Category(name='콜드브루').save()
In : Category.objects.get(id=1) Out : <Category: Category object (1)>
In : Category.objects.filter(name='탄산').update(name='콜드브루') Out : 2 #총 업데이트된 row 개수
In : Category.objects.filter(name='qp').delete() Out : (1, {'products.Category': 1})
In : category = Category.objects.get(id=2) Out : <Category: Category object (2)> In : category.name Out : '브루드커피' In : category.name = 'new name' In : category.save() In : category.name Out : 'new name'
In : Category.objects.filter(name='브루드커피').exists() Out : True
get_or_create()
bulk_create()
count()
first()
last()
aggregate()