我有两个表,一个是从病历中存储信息,另一个是第二次医疗咨询,我想要的是搜索,如果病人进行了第二次医疗咨询,在我的模板中显示了第二次医疗咨询的信息,如果病人没有第二次医疗会诊,只向我显示病历的信息。我需要帮助和想法,我是Django和Python的新手,我用以下的方式进行搜索,问题是我只看到了病历,如果病人需要第二次医疗咨询来显示信息的话。
View.py
def Expediente_Detalle(request, credencial):
Expediente_Detalle = get_object_or_404(ExpedienteConsultaInicial, credencial_consultainicial=credencial)
return render(request, 'ExpedienteDetalle.html', {'Expediente_Detalle': Expediente_Detalle})
Models.py
class ExpedienteConsultaInicial(models.Model):
credencial_consultainicial = models.CharField(max_length=10, null=True, blank=True)
def __unicode__(self):
return self.credencial_consultainicial
class ConsultasSubsecuentes(models.Model):
Consultasbc_credencial =models.ForeignKey(ExpedienteConsultaInicial,
related_name='csb_credencial')
发布于 2013-09-01 12:29:21
尝试:
#Models
class ExpedienteConsultaInicial(models.Model):
#max_legth=10 might be too small
credencial_consultainicial = models.CharField(max_length=100, null=True, blank=True)
def __unicode__(self):
return self.credencial_consultainicial
class ConsultasSubsecuentes(models.Model):
#related_name is name of attribute of instance of model
#to (not from!) which ForeignKey points.
#Like:
#assuming that `related_name` is 'consultations'
#instance = ExpedienteConsultaInicial.objects.get(
# credencial_consultainicial='text text text'
#)
#instaqnce.consultations.all()
#So I suggest to change `related_name` to something that will explain what data of this model means in context of model to which it points with foreign key.
consultasbc_credencial = models.ForeignKey(ExpedienteConsultaInicial,
related_name='consultations')
#View
def expediente_detalle(request, credencial):
#Another suggestion is to not abuse CamelCase - look for PEP8
#It is Python's code-style guide.
detalle = get_object_or_404(ExpedienteConsultaInicial, credencial_consultainicial=credencial)
subsequent_consultations = detalle.csb_credencial.all()
return render(request, 'ExpedienteDetalle.html', {'Expediente_Detalle': detalle, 'consultations': subsequent_consultations})
有用的链接:
related_name
的原因发布于 2013-09-01 12:23:20
您需要做的就是执行另一个查询,并将结果提供给模板。
def Expediente_Detalle(request, credencial):
Expediente_Detalle = get_object_or_404(ExpedienteConsultaInicial, credencial_consultainicial=credencial)
second_consultation = ExpedienteConsultaInicial.objects.filter(..)
return render(request, 'ExpedienteDetalle.html', {'Expediente_Detalle': Expediente_Detalle, 'second_consultation': second_consultation})
然后,在模板中,在second_consultation
上迭代
{% for c in second_consultation %}
<p> {{ c.credencial_consultainicial }} </p>
{% endfor %}
https://stackoverflow.com/questions/18552166
复制相似问题