django从模板中的几个表中检索字段

时间:2016-03-30 05:30:14

标签: django django-templates

class PlannedOTList(models.Model):
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    date_added = models.DateTimeField(auto_now_add=True)
    planned_surgery = models.TextField(verbose_name='diagnosis and planned surgery', blank=True) # decided by the committee
    planned_date_of_surgery = models.DateField('date of surgery', null=True, blank=True)
    planned_date_of_admission = models.DateField('date of admission', null=True, blank=True)
    remarks = models.TextField(blank=True)
    surgery_set = models.BooleanField('required surgery set', default=False)
    # to_be_admitted = models.BooleanField(default=False)
    hide = models.BooleanField(default=False)
    objects = PlannedOTListQS.as_manager()

    class Meta:
        db_table = 'planned_ot_list'
        ordering = ['-date_added']

class Admission(models.Model):
    # general info
    date_admission = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)


class OperationNotesList(models.Model):
    admission=models.ForeignKey(Admission,on_delete=models.CASCADE,null=True)
    #patient=models.ForeignKey(Patient,on_delete=models.CASCADE)
    date_added=models.DateTimeField(auto_now_add=True)
    procedure_code=models.CharField(max_length=7)
    diagnosis_code=models.CharField(max_length=10)
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, related_name='op_created_by')
    pre_operation_list=models.CharField(max_length=70,blank=True)
    intra_operation_list=models.CharField(max_length=70,blank=True)
    post_operation_list=models.CharField(max_length=70,blank=True)
    is_done=models.BooleanField(default=False)

    class Meta:
        db_table='operationNotesList'

class Patient(models.Model):
    patientid_generated_part = models.CharField(max_length=5, default='', blank=True)
    date_recorded = models.DateTimeField(default=timezone.now)
    modified = models.DateTimeField(auto_now=True, null=True)
    first_name = models.CharField(max_length=50)

    class Meta:
        db_table = 'patients'
        ordering = ['-modified']

HTML代码

<div class="row">
    <div class="col-xs-6 col-md-3"><label >Proposed Operation: &nbsp
    {{ operationnoteslist.admission.patient.planned_ot_list.planned_surgery }}</label></div>
    <div class="col-xs-6 col-md-3"><label >Weight: &nbsp
    {{ operationnoteslist.admission.weight }}&nbsp(kg)</label></div>
    <div class="col-xs-6 col-md-3"><label >Height: &nbsp
    {{ operationnoteslist.admission.height }}&nbsp(cm)</label></div>    
    <div class="col-xs-6 col-md-3"><label >BMI: &nbsp
    {{ operationnoteslist.admission.bmi }}</label></div>
</div>

上面的html代码有主模型operationnoteslist。 我试图从planned_ot_list获取值。我不知道自己错过了什么。

我认为要走的路是:MyownModelTable.foreignTablename.foreignTablename.field

建议的操作不会检索任何值。

1 个答案:

答案 0 :(得分:0)

(作为对评论的回复:planned_ot_list模型中不需要Patient

反向关系(一对多)默认具有_set后缀。此外,在您的PlannedOTList模型中,患者没有unique标志,因此患者可以有几个与之相关的标志。最重要的是,小写的模型关系名称没有下划线(驼峰情况下只是较低的情况)。所以反向关系名称应为:

patient.plannedotlist_set

(您可以使用dir(patient)打印出可用的属性,输出将包含反向关系属性。)

这将返回一个查询管理器,您不能简单地编写patient.plannedotlist_set.planned_surgery。相反,您必须决定是显示完整列表还是仅显示其中一个条目。如果他们有自然顺序而你想使用第一个或最后一个,你可以这样做:

patient.plannedotlist_set.first  # in the template or first() in view
patient.plannedotlist_set.last

要迭代所有这些,请使用:

patient.plannedotlist_set.all  # template or all() in view

请注意,您应该通过添加如下的Meta属性,为PlannedOTList提供一个排序以使其工作:

Meta:
    ordering = ('field1', 'field2', ...)  # use '-field1' for reverse

或者,如果排序依赖于视图,请在视图中排序并明确地将列表添加到模板上下文中。