2013-08-31 1 views
0

의료 기록의 정보를 저장하는 두 개의 테이블과 다른 의료 상담, 내가 원하는 것은 검색하는 것입니다. 환자가 두 번째 의학 상담을 받았다면 정보를 보여줍니다. 내 템플리트에서 두 번째 의학 상담의 경우, 환자에게 두 번째 의학 상담이없는 경우 의료 기록 정보 만 표시합니다. 내가 어떻게 할 수 있는지에 대한 도움과 아이디어가 필요하다. 장고와 파이썬을 처음 접했을 때 다음과 같은 방식으로 검색을 수행한다.이 검색의 문제점은 의료 기록 만 볼 수 있다는 것이다. 환자가 정보를 표시하기 위해 두 번째 의학 상담을받은 경우.보기에서 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}) 

유용한 링크 :

  • Following relationships backward - 나는 변화 related_name
  • PEP8 당신을하시기 바랍니다 이잖아 - 이것은 낙타 표기법에 관한 것입니다 및 파이썬 코드 스타일 .
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 %} 
관련 문제