首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >视图中的2种不同搜索

视图中的2种不同搜索
EN

Stack Overflow用户
提问于 2013-08-31 19:49:34
回答 2查看 70关注 0票数 0

我有两个表,一个是从病历中存储信息,另一个是第二次医疗咨询,我想要的是搜索,如果病人进行了第二次医疗咨询,在我的模板中显示了第二次医疗咨询的信息,如果病人没有第二次医疗会诊,只向我显示病历的信息。我需要帮助和想法,我是Django和Python的新手,我用以下的方式进行搜索,问题是我只看到了病历,如果病人需要第二次医疗咨询来显示信息的话。

View.py

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-01 12:29:21

尝试:

代码语言:javascript
运行
复制
#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})

有用的链接:

  • 向后跟踪关系 -这就是我建议你改变related_name的原因
  • PEP8 -这是关于CamelCase和Python的代码风格的。
票数 1
EN

Stack Overflow用户

发布于 2013-09-01 12:23:20

您需要做的就是执行另一个查询,并将结果提供给模板。

代码语言:javascript
运行
复制
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上迭代

代码语言:javascript
运行
复制
{% for c in second_consultation %}
    <p> {{ c.credencial_consultainicial }} </p>
{% endfor %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18552166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档