简体   繁体   中英

Django Foreign key query

I have the following modle

class PatientContact(models.Model):
    uid = models.CharField(max_length=10)
    name = models.CharField(max_length=100)
    phone = PhoneNumberField()

class Patient(models.Model):
    name = models.CharField(max_length=100)
    date_of_birth = models.DateField()
    contact = models.ForeignKey(PatientContact)

class Appointment(models.Model):
    patient = models.ForeignKey(Patient)
    time = models.DateTimeField()

I can get the list of patients registered under a user:

Patient.objects.filter(contact=uid)

How could I get the list of appointment for a user from the above model?
Used case:
The list of appointments scheduled by user (uid=1234) Not sure how to perform a backward relationship to get list of appointments for a given uid.

如果我正确地理解了您的意思,那么这并不落后,只有两个层次:

Appointment.objects.filter(patient__contact__uid=1234)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM