GioBot
GioBot

Reputation: 623

2 different search in a view

I have two tables one that stores information from a medical record and the other a second medical consultation, what I want is to make a search and if the patient had a second medical consultation shows me the information of the second medical consultation in my template, if the patient doesn't have a second medical consultation only show me the information of the medical record. I need help and ideas on how I can do it, I'm new with Django and Python , I do the search in a view in the following way, the problem with this search is that I only see the medical record and I need that if the patient had second medical consultation to display the information.

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')

Upvotes: 0

Views: 78

Answers (2)

Gill Bates
Gill Bates

Reputation: 15147

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:

Upvotes: 1

Josh Smeaton
Josh Smeaton

Reputation: 48720

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

Upvotes: 0

Related Questions