Django:根据用户实例检索数据库条目

时间:2019-04-03 16:21:02

标签: django django-models django-database

我有一个应用程序,允许患者预约医生的约会。在医生的门户上,我想检索使用该门户连接到医生的所有约会。

问题是“无法查询“人名”:必须是“医生”实例。调试后,我意识到doctor实际上是指Doctor模型中指定的upin而不是{{ 1}}。

Models.py:

username

Views.py:

class User(AbstractUser):
    users = (
        ('doctor', 'Doctor'),
        ('patient', 'Patient'),
        ('assistant', 'Assistant'),
    )

    user_type = models.CharField(choices=users, max_length=9, default='patient')

    #return the users' name
    def __str__(self):
        return self.first_name  + " " + self.last_name

class Doctor(models.Model):
    upin = models.AutoField(primary_key=True) #unique physician identification number
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user.user_type = "doctor"
    specialty = models.CharField(max_length=20)
    appointments_per_hour = models.IntegerField(null=True)
    picture = models.ImageField(upload_to = '', default = '')

    #return the doctors' name
    def __str__(self):
        return str(self.user)

class Patient(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user.user_type = "patient"
    pid = models.AutoField(unique=True, primary_key=True) #patient identification

    #return the patients' name
    def __str__(self):
        return str(self.user)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        if instance.user_type == "doctor":
            Doctor.objects.create(user=instance)
        elif instance.user_type == "patient":
            Patient.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    if instance.user_type == "doctor":
        instance.doctor.save()
    elif instance.user_type == "patient":
        instance.patient.save()

class Appointment(models.Model):
    TIMESLOT_LIST = (
        (1, '10:00 – 11:00'),
        (2, '11:00 – 12:00'),
        (3, '12:00 – 13:00'),
        (4, '13:00 – 14:00'),
        (5, '14:00 – 15:00'),
        (6, '15:00 – 16:00'),
        (7, '16:00 – 17:00'),
        (8, '17:00 – 18:00'),
        (8, '18:00 – 19:00'),
    )

    timeslot = models.IntegerField(choices=TIMESLOT_LIST, null=True)
    doctor = models.ForeignKey(Doctor, on_delete = models.CASCADE, related_name='doctor')
    patient = models.ForeignKey(Patient, on_delete = models.CASCADE, related_name='patient')


    def __str__(self):
        return str(self.doctor) + " " + str(self.patient) + " - " + str(self.get_timeslot_display()) 

对于查询条件,我可以使用def doctor_portal(request): appointments = Appointment.objects.get(doctor=request.user) return TemplateResponse(request, 'scheduling/doctor.html', {"appointments" : appointments}) doctor。正如预期的那样,doctor.id是指Doctor模型中的doctor.id。但是,为什么upin不是指医生的doctor

1 个答案:

答案 0 :(得分:0)

您的Appointment模型字段doctor = models.ForeignKey(Doctor, on_delete = models.CASCADE, related_name='doctor')链接到Doctor,而不是User模型。

所以尝试一下:

def doctor_portal(request):
    appointments = Appointment.objects.get(doctor=request.user.doctor)
    return TemplateResponse(request, 'scheduling/doctor.html', {"appointments" : appointments})