1
| pip install django_extensions
|
1
2
| python manage.py shell_plus
# 이렇게 하면 django_extensions으로 인해 설치했던 models들을 자동으로 import해줌
|
1
2
3
4
5
6
7
8
9
10
11
| (1)
user = User.objects.create(name="싸피")
(2)
user = User(name="싸피")
user.save()
(3)
user = User()
user.name = "싸피"
user.save()
## 모두 같은 결과
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| (1)
# user라는 정보를 통째로 넘김
post = Post.objects.create(title="안녕하세요", user=user)
(2)
p = Post()
p.title="안녕하세요"
p.user = user
p.save()
(3)
# user_id가 가능
post = Post.objects.create(title="안녕하세요", user_id=1)
## user에는 instance객체가 들어와야 하고 user_id에는 숫자가 와야 함
|
1
2
3
4
| (1)
Comment.objects.create(content="반갑습니다", post_id=1, user_id=1)
(2)
Comment.objects.create(content="반갑습니다", post=Post.objects.get(id=1), user_id=1)
|
1
2
3
4
5
6
7
8
9
10
11
12
| user1 = User.objects.create(name='Kim')
user2 = User.objects.create(name='Lee')
post1 = Post.objects.create(title='1글', user=user1)
post2 = Post.objects.create(title='2글', user=user1)
post3 = Post.objects.create(title='3글', user=user2)
c1 = Comment.objects.create(content='1글1댓글', user=user1, post=post1)
c2 = Comment.objects.create(content='1글2댓글', user=user2, post=post1)
c3 = Comment.objects.create(content='1글3댓글', user=user1, post=post1)
c4 = Comment.objects.create(content='1글4댓글', user=user2, post=post1)
c5 = Comment.objects.create(content='2글1댓글', user=user1, post=post2)
c6 = Comment.objects.create(content='!1글5댓글', user=user2, post=post1)
c7 = Comment.objects.create(content='!2글2댓글', user=user2, post=post2)
|
1
2
3
4
| (1)
User.objects.get(id=1)
(2)
User.objects.get(pk=1)
|
1
2
3
| (1)
user1.post_set.all()
# QuerySet 형식(리스트와 흡사)
|
1
2
3
| for post in user1.post_set.all():
for comment in post.comment_set.all():
print(comment.content)
|
1
2
3
4
5
| c2.user.post_set.all()
or
user2.post_set.all()
or
User.objects.get(name=c2.user.name).post_set.all()
|
1
2
3
4
5
| post1.comment_set.all()[0].user.name
# QuerySet이기 때문에 리스트 접근 가능
or
post1.comment_set.first().user.name
# first, last만 있음
|
1
| post1.comment_set.all()[1:4]
|
1
| post1.comment_set.all()[1].user.post_set.all()[0].user.name
|
1
2
3
4
| (1)
c1.user
(2)
Comment.objects.values('user').get(id=1)
|
1
2
3
| user2.comment_set.order_by('-content')
or
user2.comment_set.order_by('-content').all()
|
1
2
3
| Post.objects.get(title='1글') # 첫번째 값
or
Post.objects.filter(title='1글') # 모두 가져와
|
1
| Post.objects.filter(title__contains="글") # 언더바 2개
|
1
| Comment.objects.filter(post__title='1글')
|
1
| Comment.objects.filter(post__title__contains="1")
|
1
2
3
4
5
| (1)
user=User(name="동식")
user.save()
(2)
User.objects.create(name="동식")
|
1
2
3
4
5
6
7
| (1)
Profile.objects.create(user=user, nickname="동동")
(2)
profile= Profile()
profile.nickname="동동"
profile.user = user
profile.save()
|
1
2
3
4
5
| Doctor.objects.create(name="Kim")
Patient.objects.create(name="Park")
p = Patient.objects.get(name="Park")
d = Doctor.objects.get(name="Kim")
Reservation.objects.create(doctor=d, patient=p)
|
1
2
3
| d.reservation_set.all() # 예약차트
for r in d.reservation_set.all():
r.patient.name
|
1
2
3
4
5
6
| p = Patient.objects.get(id=1)
p.reservation_set.all()
>>> <QuerySet [<Reservation: Reservation object (1)>]>
p.doctors.all()
>>> <QuerySet [<Doctor: Doctor object (1)>]>
|
1
2
| d = Doctor.objects.get(id=1)
d.patient_set.all()
|
1
2
3
4
5
| d = Doctor.objects.create(name="kim")
p = Patient.objects.create(name="박")
p2 = Patient.objects.create(name="이")
d.patients.add(p,p2) # 연결
d.patients.remove(p) # 삭제
|