[Answer]-2 different search in a view




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.
     #assuming that `related_name` is 'consultations'
     #instance = ExpedienteConsultaInicial.objects.get(
     #                     credencial_consultainicial='text text text'
     #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,


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:


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 %}

Leave a comment