WEB/Django prac

[Django prac][DB N:M ① 의사와 환자] 9. 예약 생성

bay07 2024. 4. 8. 16:25

1. shell_plus 들어가기

python manage.py shell_plus

 

2. 의사, 환자 만들기 

doctor1 = Doctor.objects.create(name='allie')
patient1 = Patient.objects.create(name='carol')
patient2 = Patient.objects.create(name='duke')

 

3. Reservation class를 통한 예약 생성

# Reservation class를 통한 예약 생성
# 의사, 환자, 증상명 이렇게 넣어야지 예약이 만들어진다. 
reservation1 = Reservation(doctor=doctor1, patient=patient1, symptom='headache')
reservation1.save()
doctor1.patient_set.all()
patient1.doctors.all()

 

4. Patient 객체를 통한 예약 생성

# Patient 객체를 통한 예약 생성
# 2번 환자가 1번 의사에게 예약하기
# 증상을 같이 넣어줘야지 예약이 된다
patient2.doctors.add(doctor1, through_defaults={'symptom': 'flu'})
doctor1.patient_set.all()
patient2.doctors.all()

 

5. 예약한 것 지우기

doctor1.patient_set.remove(patient1)
patient2.doctors.remove(doctor1)