我有两个模特医生和病人有多对多的关系。 Doctor和Patient都是我系统的用户,所以我想使用一对一的关系将它与User模块联系起来。医生模型与多对多医院有另一种关系。
以下是我的代码:
from django.contrib.auth.models import User
from django.db import models
from datetime import date
class Doctor(models.Model):
photo = models.ImageField(upload_to='/uploads/photos', default='/uploads/photos/doctor_default_photo.gif')
degree = models.CharField(max_length=200)
speciality = models.CharField(max_length=200)
educational_institute = models.CharField(max_length=200)
experience = models.IntegerField(null=True)
user = models.OneToOneField(User)
class Patient(models.Model):
user = models.OneToOneField(User)
disease = models.CharField(max_length=100, null=True)
dob = models.DateField('date of birth')
doctors = models.ManyToManyField(Doctor, through='Appointment')
def patient_age(self):
today = date.today()
year, month, day = map(int, self.dob.split('-'))
return today.year - year - ((today.month, today.day) < (month, day))
class Appointment(models.Model):
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
date = models.DateField()
time = models.TimeField()
但是,当我运行migrate命令时,我得到以下错误。
psycopg2.IntegrityError:column&#34; user_id&#34;包含空值