1👍
✅
Try:
#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})
Useful links:
- Following relationships backward – thats why I suggest you to
changerelated_name
- PEP8 – and this is about CamelCase and
Python’s code-style.
0👍
All you need to do is to execute another query, and provide the result to your template
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})
Then, in your template, iterate over second_consultation
:
{% for c in second_consultation %}
<p> {{ c.credencial_consultainicial }} </p>
{% endfor %}
- [Answer]-Django 1.6 adding rows o excel sheet
- [Answer]-Unable to use admin's static file when debug is False in django
Source:stackexchange.com