如何使用django查询两个表中的数据?
How to query data from two tables using django?
我有 mySQL table 与“一对多”关系有关
主要 table 的参数是:
- 姓名
- 年龄
- 身高
第二个 table 的参数是:
- f_name(作为第一个 table 的外键)
- salary_date
- 工资
所以我需要做类似的事情:
select * from table2
where t_id in( select t_id from table1
where t_var = 'Alfred'
);
我已经使用此代码从第一个 table 获取数据:
models.py
from tabnanny import verbose
from django.db import models, connections
from django.urls import reverse
class collection_data(models.Model):
name = models.CharField(max_length=50)
age =models.IntegerField()
height = models.IntegerField()
class Meta:
verbose_name = 'Collection Data'
class second_data(models.Model):
# connect this table with previos
data = models.ForeignKey(collection_data, on_delete=models.CASCADE)
salary= models.FloatField(max_length=100)
salary_date= models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = 'Frequent Collection Data'
ordering = ['salary_date']
views.py
from django.views.generic import DetailView
class InfoDetailView(DetailView):
model = collection_data
template_name = 'info/Info.html'
context_object_name = 'info'
urls.py
path('<int:pk>', views.InfoDetailView.as_view(), name="info-data"),
Info.html
<ul class="data">
<li>{{ info.name }}</li>
<li>{{ info.age }}</li>
<li>{{ info.height}}</li>
</ul>
<table>
<tr>
<th>Date</th>
<th>Salary</th>
</tr>
{% for el in second_data %}
<tr>
<td>05/06/2021</td>
<td>1350$</td>
</tr>
{% endfor %}
</table>
页面上的结果必须是:
如果要检索模板中与collection_data
相关的所有second_data
个对象,您需要使用{% for el in info.second_data_set.all %}
而不是{% for el in second_data %}
。考虑设置 models.ForeignKey()
的 related_name
属性,这样你就可以做类似 {% for el in info.'related_name'.all %}
.
的事情
也尝试使用 CamelCase 命名 class(CollectionData
而不是 collected_data
)。
query = second_data.objects.all()
然后在你的html文件中像这样写
{% for item in query %}
{{ item.salary }}
{{ item.salary_date }}
{{ endfor }}
我有 mySQL table 与“一对多”关系有关
主要 table 的参数是:
- 姓名
- 年龄
- 身高
第二个 table 的参数是:
- f_name(作为第一个 table 的外键)
- salary_date
- 工资
所以我需要做类似的事情:
select * from table2
where t_id in( select t_id from table1
where t_var = 'Alfred'
);
我已经使用此代码从第一个 table 获取数据:
models.py
from tabnanny import verbose
from django.db import models, connections
from django.urls import reverse
class collection_data(models.Model):
name = models.CharField(max_length=50)
age =models.IntegerField()
height = models.IntegerField()
class Meta:
verbose_name = 'Collection Data'
class second_data(models.Model):
# connect this table with previos
data = models.ForeignKey(collection_data, on_delete=models.CASCADE)
salary= models.FloatField(max_length=100)
salary_date= models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name = 'Frequent Collection Data'
ordering = ['salary_date']
views.py
from django.views.generic import DetailView
class InfoDetailView(DetailView):
model = collection_data
template_name = 'info/Info.html'
context_object_name = 'info'
urls.py
path('<int:pk>', views.InfoDetailView.as_view(), name="info-data"),
Info.html
<ul class="data">
<li>{{ info.name }}</li>
<li>{{ info.age }}</li>
<li>{{ info.height}}</li>
</ul>
<table>
<tr>
<th>Date</th>
<th>Salary</th>
</tr>
{% for el in second_data %}
<tr>
<td>05/06/2021</td>
<td>1350$</td>
</tr>
{% endfor %}
</table>
页面上的结果必须是:
如果要检索模板中与collection_data
相关的所有second_data
个对象,您需要使用{% for el in info.second_data_set.all %}
而不是{% for el in second_data %}
。考虑设置 models.ForeignKey()
的 related_name
属性,这样你就可以做类似 {% for el in info.'related_name'.all %}
.
也尝试使用 CamelCase 命名 class(CollectionData
而不是 collected_data
)。
query = second_data.objects.all()
然后在你的html文件中像这样写
{% for item in query %}
{{ item.salary }}
{{ item.salary_date }}
{{ endfor }}