reservation = Reservation.objects.get(pk=1)
reservation.symptom = 'x'
reservation.save()
reservation = Reservation()
reservation.symptom='a'
reservation.patient_id=1
reservation.doctor_id=1
reservation.save()
class Doctor(models.Model):
name = models.TextField()
def __str__(self):
return f'{self.pk}번 의사 {self.name}'
class Patient(models.Model):
doctors = models.ManyToManyField(Doctor, through='Reservation')
name = models.TextField()
def __str__(self):
return f'{self.pk}번 환자 {self.name}'
class Reservation(models.Model):
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
#증상
symptom = models.TextField()
#예약일자
reserved_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'{self.doctor.pk}번 의사의 {self.patient.pk}번 환자'
doctor1 = Doctor.objects.create(name='justin')
patient1 = Patient.objects.create(name='tony')
patient2 = Patient.objects.create(name='harry')
#예약 만들기(증상을 담아서)
reservation1 = Reservation(doctor=doctor1, patient=patient1, symptom='headache')
#예약 저장
reservation1.save()
#의사의 예약
doctor1.patient_set.all()
patient1.doctors.all()
#환자의 입장에서 예약 만들기
patient2.doctors.add(doctor1, through_defaults={'symptom': 'flu'})
doctor1.patient_set.all()
patient2.doctors.all()
테이블 확인
예약 삭제
doctor1.patient_set.remove(patient1)
patient2.doctors.remove(doctor1)
from <model>_id
to_<model>_id