Py学习  »  Django

DJANGO中如何从关系数据库中检索数据

Regnald Terry • 4 年前 • 378 次点击  

此儿童详细模型与临床检验模型有关

class Child_detail(models.Model):
    Firstname = models.CharField(max_length = 50)
    Lastname = models.CharField(max_length = 50)
    Tribe = models.CharField(max_length = 50)
    Religion = models.CharField(max_length = 50)
    Date_of_birth = models.DateField()
    Current_Address = models.CharField(max_length = 50)


    def __str__(self):
        return self.Firstname

这是我想从中检索数据的临床检验模型

class Clinical_test(models.Model):
    child = models.ForeignKey(Child_detail, on_delete = models.CASCADE) 
    Test_type = MultiSelectField(max_length=100,choices=test_type,max_choices=30)
    Test_date = models.DateTimeField()
    Next_schedule_test_date = models.DateField(blank=True)

    def __str__(self):
        return str(self.Test_type)

这是我的观点.py

def more_about_child(request,pk):
    child = get_object_or_404(Child_detail,pk=pk)
    context={
        'child':child,        
    }
    return render(request,'functionality/more.html',context)

这是我的template.html,它将用于显示检索到的数据

<div class="container" style="margin-top:20px">
    <div class="col-md-12" style="background-color:rgb(44, 44, 3);color:white">
        <h4>clinical test</h4>
    </div>
    <div class="row">
        <div class="col-md-3">
            <p>First name: <b>{{clinical.child.Test_date}}</b> </p>
        </div>
        <div class="col-md-3">
            <p>Last name: <b>{{child.Lastname}}</b> </p>
        </div>
        <div class="col-md-3">
            <p>Tribe: <b>{{child.Tribe}}</b> </p>
        </div>
        <div class="col-md-3">
            <p>Religion: <b>{{child.Religion}}</b> </p>
        </div>
        <div class="col-md-3">
            <p>Religion: <b>{{child.Date_of_birth}}</b> </p>
        </div>
        <div class="col-md-3">
            <p>Religion: <b>{{child.Current_Address}}</b> </p>
        </div>
    </div>
</div>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56588
 
378 次点击  
文章 [ 1 ]  |  最新文章 4 年前
yash
Reply   •   1 楼
yash    4 年前

以下是逐步解决方案(就我所知,您需要什么)

  1. 首先使用pk从子detail类查询子对象

    child_detail=子_detail.objects.get(pk=pk)

  2. 现在,来自临床检验的查询

在这之后,你可以发送 child