更新
我将查询更改为:
ClinicDoctorDayShift.objects.filter(clinic_doctor__doctor_id = doctor_id)
有了这个,我就能得到所有的价值。但正如您所看到的,我现在没有使用prefectch
。如果我必须通过名字获得像filter
这样的许多医生,这是否效率低下?
模板现在有这个:
{% for vis in visitinghours %}
<tr>
<td>
{{ vis.clinic_doctor.doctor.full_name }}
</td>
<td>
{{ vis.clinic_doctor.clinic.name }}
</td>
<td>
{{ vis.day_shift.day.day }}
</td>
<td>
{{ vis.day_shift.time }}
</td>
</tr>
{% endfor %}
ORIGINAL
第一个网站。第一次使用Django。 我现在正在使用此查询:
visitingQuerySet = ClinicDoctor.objects.filter(doctor_id = doctor_id).prefetch_related('clinicDoctor_dayShift').all()
然后在模板中我想要这样的东西:
{% for vis in visitinghours %}
<tr>
<td>
{{ vis.doctor.full_name }}
</td>
<td>
{{ vis.clinic.name }}
</td>
<td>
{{ vis.clinicdoctordayshift.id }}
</td>
</tr>
I can get first two `<td>` but not others if I try to get values from other models.
我的模特是:
class User(AbstractUser):
full_name = models.CharField(max_length=200)
clinics = models.ManyToManyField(Clinic, through='ClinicDoctor', related_name='doctors')
class ClinicDoctor(models.Model):
doctor = models.ForeignKey('User', related_name='doctorsF')
clinic = models.ForeignKey(Clinic, related_name='clinicsF')
class Day(models.Model):
day = models.CharField(max_length=20)
class Shift(models.Model):
shift = models.CharField(max_length=20)
days = models.ManyToManyField(Day, through='DayShift', related_name='day_shift')
class DayShift(models.Model):
time = models.TimeField()
day = models.ForeignKey(Day, related_name='to_day')
shift = models.ForeignKey(Shift, related_name='to_shift')
clinics_doctors = models.ManyToManyField(ClinicDoctor, through='ClinicDoctorDayShift', related_name='clinicDoctor_dayShift')
class ClinicDoctorDayShift(models.Model):
clinic_doctor = models.ForeignKey(ClinicDoctor, related_name='to_clinicDoctor')
day_shift = models.ForeignKey(DayShift, related_name='to_dayShift')
我希望能够获得Day.day
,Shift.shift
和DayShift.time
。
我的prefetch_related
错了吗?
在prefetch_related
:
if visitingQuerySet:
我的意思是这会导致任何额外的DB
点击,或者这将是实际评估它的第一个点吗?
目前,我可以看到为该部分执行的这两个查询:
SELECT
health_clinicdoctor.id,
health_clinicdoctor.doctor_id,
health_clinicdoctor.clinic_id,
health_clinicdoctor.is_active
FROM health_clinicdoctor
WHERE health_clinicdoctor.doctor_id = 14
SELECT
(health_clinicdoctordayshift.clinic_doctor_id) AS _prefetch_related_val_clinic_doctor_id,
health_dayshift.id,
health_dayshift.time,
health_dayshift.day_id,
health_dayshift.shift_id
FROM health_dayshift
INNER JOIN health_clinicdoctordayshift
ON ( health_dayshift.id = health_clinicdoctordayshift.day_shift_id )
WHERE health_clinicdoctordayshift.clinic_doctor_id IN (1, 2, 3)
请帮助。
由于