2013-08-31 25 views
0

我有兩個表一個存儲病歷信息和另一個第二個醫療諮詢,我想要的是進行搜索,如果患者有第二次醫療諮詢顯示我的信息我的模板中的第二次醫療諮詢,如果患者沒有第二次醫療諮詢,只會顯示醫療記錄的信息。 我需要關於如何做到這一點的幫助和想法,我是Django和Python的新手,我按照以下方式在視圖中進行搜索,此搜索的問題是我只能看到病歷,而且我需要如果病人有第二次醫療諮詢來顯示信息。2個不同的搜索視圖

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

張貼您的模特 –

回答

1

嘗試:

#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}) 

相關鏈接:

0

所有你需要做的是執行另一個查詢,並將結果提供給您的模板

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 %} 
相關問題