Django从多个表中检索数据

Django从多个表中检索数据

本文介绍了Django从多个表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个MySQL模型:

 类注册(models.Model):
name = models.CharField (max_length = 30)
email = models.EmailField()
password = models.CharField(max_length = 30)
company = models.CharField(max_length = 30)


class personal_details(models.Model):
reg = models.ForeignKey(registration)
job = models.CharField(max_length = 30)
experience = models.IntegerField我想使用体验和公司作为关键字来过滤细节。我想在我的HTML页面中从两个表中获取并显示详细信息(姓名,电子邮件,公司,工作,经验)。

解决方案
  details = personal_details.objects.filter(experience = 1,
reg__company =YourCompany)。select_related()

{%详细信息%}
名称:{{detail.reg.name}}
电子邮件:{{detail.reg.email}}
公司:{{detail.reg.company}}
Job:{{detail.job}}
体验:{{detail.experience}}< br />
{%endfor%}


I have two MySQL models:

class registration(models.Model):
   name = models.CharField(max_length=30)
   email = models.EmailField()
   password = models.CharField(max_length=30)
   company = models.CharField(max_length=30)


class personal_details(models.Model):
   reg = models.ForeignKey(registration)
   job = models.CharField(max_length=30)
   experience = models.IntegerField(default=0)

I want to filtering details using both experience and company as a keyword. I want to fetch and display details(name, email, company, job, experience) from both tables in my HTML page.

解决方案
details = personal_details.objects.filter(experience=1, 
    reg__company="YourCompany").select_related()

{% for detail in details %}
    Name: {{ detail.reg.name }}
    Email: {{ detail.reg.email }}
    Company: {{ detail.reg.company }}
    Job: {{ detail.job }}
    Experience: {{ detail.experience }}<br/>
{% endfor %}

这篇关于Django从多个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:37