자료형 변환
이나 serializers
가 필요>>> from user.models import User
>>> user = User.objects.all()
>>> user
<QuerySet [<User: User object (1)>, <User: User object (2)>]>
>>> type(user)
<class 'django.db.models.query.QuerySet'>
objects
는 Model Managerobjects
를 통해 반환되는 객체가 QuerySet
INSERT
절>>> from user.models import User
>>> User.objcets(first_name="Inyong", last_name="Pang")
>>> from user.models import User
>>> user = User(first_name="Inyong", last_name="Pang")
>>> user.save()
SELECT
절objects
를 통해 가져온 QuerySet에서 조건에 따른 객체를 가져오는 메소드>>> User.objects.all()
<QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
>>> User.objects.values()
<QuerySet [{'id': 1, 'name': '첫째', 'email': 'test01@gmail.com'}, {'id': 2, 'name': '둘째', 'email': 'test02@gmail.com'}, {'id': 3, 'name': '셋째', 'email': 'test03@gmail.com'}, {'id': 4, 'name': '넷째', 'email': 'test04@gmail.com'}, {'id': 5, 'name': '다섯째', 'email': 'test05@gmail.com'}]>
>>> User.objects.values_list()
<QuerySet [(1, '첫째','test01@gmail.com'), (2, '둘째', 'test02@gmail.com'), (3, '셋째', 'test03@gmail.com'), (4, '넷째', 'test04@gmail.com'), (5, '다섯째', 'test05@gmail.com')]>
>>> user = User.objects.all()
<QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
>>> user.first()
<User: User object (1)>
>>> user = User.objects.all()
<QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
>>> user.last()
<User: User object (5)>
>>> user = User.objects.all()
<QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
>>> user.count()
5
.get()
뒤에 다른 메소드를 추가할 수 없음.get()
의 매개변수로 지정DoesNotExist
메세지 발생MultipleObjectsReturned
메세지 발생>>> from user.models import User
>>> user = User.objects.get(email="test01@email.com")
<User: User object (1)>
.filter()
의 매개변수로 지정DoesNotExist
메세지 발생>>> from user.models import User
>>> User.objects.filter(is_use=True)
<QuerySet [<User: User object (1)>, <User: User object (2)>]>
.exlude()
의 매개변수로 지정>>> from user.models import User
>>> User.objects.exclude(is_use=False)
<QuerySet [<User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
.filter()
메소드 사용 시, 두개 이상의 조건을 AND 또는 OR로 사용가능Q
객체가 필요AND 조건
>>> User.objects.get(email="test01@gmail.com", name="InyongPang")
<User: User object (2)>
>>> User.objects.filter(last_name="Pang", is_use=True)
<QuerySet [<User: User object (2)>, <User: User object (3)>]>
>>> import django.db.models import Q
>>> User.objects.filter(Q(last_name="Pang") & Q(is_use=True))
<QuerySet [<User: User object (2)>, <User: User object (3)>]>
OR 조건
>>> import django.db.models import Q
>>> User.objects.filter(Q(last_name="Pang") | Q(is_use=False))
<QuerySet [<User: User object (2)>, <User: User object (3)>], <User: User object (4)>], <User: User object (5)>]>
UPDATE
절>>> user = User.objects.get(email="test01@email.com")
>>> user.name
'Pang'
>>> user.name = "Pang_modify"
>>> user.save()
>>> user.name
'Pang_modify'
>>> user = User.objects.filter(is_use=True)
>>> user
<QuerySet [<User: User object (1)>, <User: User object (2)>]>
>>> user.update(is_use=False)
>>> use_user = User.objects.filter(is_use=True)
>>> use_user
<QuerySet []>
DELETE
절# filter 조건에 의한 레코드 삭제
>>> user = User.objects.filter(is_use=True)
>>> user
<QuerySet [<User: User object (1)>, <User: User object (2)>]>
>>> user.delete()
>>> user
<QuerySet [<User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
# all 조건에 의한 모든 레코드 삭제
>>> user = User.objects.all()
<QuerySet [<User: User object (1)>, <User: User object (2)>, <User: User object (3)>, <User: User object (4)>, <User: User object (5)>]>
>>> user.delete()
>>> user
<QuerySet []>