我是Django的新手,我想对如何从3个表进行查询提供一些建议。
我有3个表从旧数据库映射到模型(Patient
,PrescribedMeds
,PrescribedMedsSchedule
)。我们无法更改此结构,因为在创建Django应用程序时必须保持活动状态。
1名患者可以服用许多处方药。1种处方药可以在计划中多次服药
下面是Django中的模型。
class Patient(models.Model):
patient_name = models.CharField(db_column='patient_name', max_length=50)
dob = models.DateTimeField(db_column='DOB', blank=True, null=True) # Field name made lowercase.
gender = models.CharField(db_column='Gender', max_length=7) # Field name made lowercase.
dateofentry = models.DateTimeField(db_column='DateOfEntry', blank=True, null=True) # Field name made lowercase.
....
....
class Meta:
managed = False
db_table = 'patient'
def __str__(self):
return self.patient_name
class PrescribedMeds(models.Model):
#id = models.AutoField(db_column='ID', primary_key=True) # Field name made lowercase.
patient_id= models.ForeignKey(Patient, models.DO_NOTHING, db_column='patient_id')
med_type = models.SmallIntegerField(db_column='Type') # Field name made lowercase.
name_of_medication = models.CharField(db_column='Name_Of_Medication', max_length=50, blank=True, null=True) # Field name made lowercase.
rxno = models.CharField(db_column='RxNo', max_length=50, blank=True, null=True) # Field name made lowercase.
date_filled = models.DateTimeField(db_column='DateFilled', blank=True, null=True) # Field name made lowercase.
....
....
class Meta:
managed = False
db_table = 'prescribed_meds'
def __str__(self):
return str(self.id) + ", " + self.name_of_medication + ", " + str(self.childno)
class PrescribedMedsSchedule(models.Model):
#id = models.AutoField(db_column='ID', primary_key=True) # Field name made lowercase.
prescribed_meds_id = models.ForeignKey(PrescribedMeds, models.DO_NOTHING, db_column='prescribed_meds_ID') # Field name made lowercase.
medication_date = models.DateField()
medication_time = models.DateTimeField()
quantity = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
form = models.CharField(max_length=1, blank=True, null=True)
class Meta:
managed = False
db_table = 'prescribed_meds_schedule'
[我正在尝试在Django中获得正确的语法,以显示3列(Prescribed_Meds.ID
,PrescribedMeds.name_of_medication
,Patient.patient_name
,PrescribedMedsSchedule.medication_date
,PrescribedMedsSchedule.medication_time
中的数据。
在SQL中,查询为
SELECT prescribed_meds.ID, prescribed_meds.Name_Of_Medication, patient.patient_name, prescribed_meds_schedule.medication_date, prescribed_meds_schedule.medication_time
FROM prescribed_meds_schedule
INNER JOIN (prescribed_meds INNER JOIN patient ON prescribed_meds.patient_id = patient.id) ON prescribed_meds_schedule.prescribed_meds_ID = prescribed_meds.ID;
在Django中正确的查询是什么?我遇到了一个问题,因为从PrescribedMedsSchedule
到Patient
表没有关系。
我尝试了以下操作:
my_obj = PrescribedMedsSchedule.objects.all().selected_related(
'prescribed_meds_id'
).prefetch_related('PrescribedMeds__patient_id')
但是,此查询不会显示Patient
表/模型。
任何建议都将不胜感激。
值得从django在FK内部进行的操作开始。您的_id
模型中有一个后缀,但是django会自动在数据库列中执行此后缀。因此,在您的模型中,您可能会更容易阅读字段patient = models.ForeignKey(Patient)
,它将在数据库中为patient_id
。
select_related
遵循外键关系,所以您这样做是对的。如果要INNER join,则应按外键。在您的情况下,请使用select_related
通过模型:
double-underscore
或者您可以使用2个查询,并使用PrescribedMedsSchedule.objects.all().select_related(
'prescribed_meds_id'
).select_related('prescribed_meds_id__patient_id')
中的id
值来查询PrescribedMeds
。就像这样;
PrescribedMedsSchedule