我想在模板中显示外键的值,但是在尝试渲染时它们显示为空白。我要显示的值是来自Paciente属性的值,该属性通过外键连接到另一个应用程序中的Patient模型,我将展示我为实现这一目标而进行的尝试,但是失败了,请您帮我还是告诉我我做错了什么:(
class AppointmentIndexView(ListView):
model = Consults
template_name = 'appointments_index.html'
context_object_name = 'consults'
ordering = ['Fecha']
def get_queryset(self):
queryset = super().get_queryset()
queryset.filter(Fecha=datetime.date.today())
return queryset
class Consults(models.Model):
#General Consult Info
Paciente = models.ForeignKey(Patient,on_delete=models.CASCADE)
Fecha = models.DateField()
Motivo = models.CharField(max_length=500,null=True)
Padecimiento = models.CharField(max_length=500,null=True)
#Main Patient Info
Presion = models.CharField(max_length=20,blank=True,null=True)
Temperatura = models.FloatField(blank=True,null=True)
Peso = models.FloatField(blank=True,null=True)
Talla = models.FloatField(blank=True,null=True)
#Any Exams done before
Estudios = models.ImageField(upload_to='studies',blank=True)
#Interrogatory by System
Digestivo = models.CharField(max_length=500,blank=True,null=True)
Endocrino = models.CharField(max_length=500,blank=True,null=True)
Renal = models.CharField(max_length=500,blank=True,null=True)
Linfativo = models.CharField(max_length=500,blank=True,null=True)
Respiratorio = models.CharField(max_length=500,blank=True,null=True)
#Physical Exploration
Cabeza = models.CharField(max_length=500,blank=True,null=True)
Torax = models.CharField(max_length=500,blank=True,null=True)
#Diagnose
CIE_10 = models.ForeignKey(CIE_10,on_delete=models.DO_NOTHING,blank=True,null=True)
Detalle_de_Codigo = models.CharField(max_length=500,blank=True,null=True)
Diagnostico = models.CharField(max_length=500,blank=True,null=True)
Procedimiento = models.CharField(max_length=500,blank=True,null=True)
Analisis = models.CharField(max_length=500,blank=True,null=True)
#Treatment
Medicamento = models.CharField(max_length=500,blank=True,null=True)
Descripcion = models.CharField(max_length=500,blank=True,null=True)
Uso = models.CharField(max_length=500,blank=True,null=True)
Dosis = models.CharField(max_length=500,blank=True,null=True)
Acciones = models.CharField(max_length=500,blank=True,null=True)
{%extends 'base.html'%}
{%load staticfiles%}
{%block body_block%}
<link rel="stylesheet" href="{%static 'appointments/css/appointments_index.css'%}">
{%if consults%}
<h1 id="Heading">Consultas <h5 id="Date">{% now "j F Y" %}</h5></h1>
<table>
<thead>
<th>Codigo de Paciente</th>
<th>Paciente</th>
<th>Fecha</th>
<th>Motivo</th>
<th>Padecimiento</th>
</thead>
<tbody>
{%for consult in consults%}
{%for patient in consults.Patient.all%}
<tr>
<td>{{patient.Codigo}}</td>
<td>{{patient.Nombres}} {{patient.Apellidos}}</td>
<td>{{consult.Fecha}}</td>
<td>{{consult.Motivo}}</td>
<td>{{consult.Padecimiento}}</td>
</tr>
{%endfor%}
{%endfor%}
</tbody>
</table>
{%else%}
<h1 id="noregisters">No existen registros de consultas aun.</h1>
{%endif%}
<button id="Add"><a class="fas fa-plus" href="{%url 'addappointment'%}"></a></button>
{%endblock%}
答案 0 :(得分:1)
Paciente
模型中的 Consult
对象关系是不可迭代的。这种关系是一对多关系,这意味着耐心有许多Consult
。
class Consults(models.Model):
Paciente = models.ForeignKey(Patient,on_delete=models.CASCADE, related_name="consults")
您可以从consults
个对象迭代Paciente
向后关系
instance = Paciente.objects.first()
for consult in instance.consults.all():
# Do everyting with consult object
您也可以在模板中迭代向后关系
{% for consult in instance.consults.all %}
# Do everyting with consult object
{% endfor %}
或者您可以执行以下操作直接调用Paciente
对象
{%for consult in consults%}
<tr>
<td>{{consult.Paciente.Codigo}}</td>
<td>{{patient.Paciente.Nombres}} {{patient.Paciente.Apellidos}}</td>
<td>{{consult.Fecha}}</td>
<td>{{consult.Motivo}}</td>
<td>{{consult.Padecimiento}}</td>
</tr>
{%endfor%}
答案 1 :(得分:0)
{%for patient in consults.Patient.all%}
应该是{% for patient in consult.patient_set.all %}
答案 2 :(得分:0)
您需要将内部for循环从{%for patient in consults.Patient.all%}
更改为{% for patient in consults.Paciente_set.all %}
。您可以通过在ForeignKey定义中添加Paciente_set
属性来更改related_name
的名称。