Can';t组合不同的查询集并在django中呈现它们
因此,我必须在html模板上呈现一个表,该模板组合了来自两个不同基础模型的列。但是这张桌子看起来不太好。来自其他表的条目显示在不同的行中 -为此,我使用了ittertools的chain方法。不知道我在哪里搞砸了 Models.py:Can';t组合不同的查询集并在django中呈现它们,django,django-models,django-templates,django-queryset,Django,Django Models,Django Templates,Django Queryset,因此,我必须在html模板上呈现一个表,该模板组合了来自两个不同基础模型的列。但是这张桌子看起来不太好。来自其他表的条目显示在不同的行中 -为此,我使用了ittertools的chain方法。不知道我在哪里搞砸了 Models.py: class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) first_name = models.CharField(max
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
image = models.ImageField(upload_to='profile_pics')
dob = models.DateField()
email = models.EmailField(max_length=50, unique=True)
class Placement(models.Model):
student = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.CharField(max_length=40)
position = models.CharField(max_length=40)
city = models.CharField( max_length=40)
bond = models.CharField( max_length=40)
ctc = models.CharField( max_length=40)
my Views.py:
def mentorintern(request):
table = Placement.objects.all()
name_student = Profile.objects.all()
in_table = list(chain(table, name_student))
return render(request, 'mentorinternship.html', {'in_table': in_table })
用于呈现表格的HTML模板:
<table class="table align-items-center table-flush table-hover" id="dataTableHover">
<thead class="thead-light">
<tr>
<th>Name of Student</th>
<th>Company</th>
<th>Position</th>
<th>City</th>
<th>CTC</th>
<th>Bond</th>
</tr>
</thead>
<tbody>
{% for std in in_table %}
<tr>
<td>{{std.first_name}}</td>
<td>{{std.company}}</td>
<td>{{std.position}}</td>
<td>{{std.city}}</td>
<td>{{std.ctc}}</td>
<td>{{std.bond}}</td>
</tr>
{% endfor %}
</tbody>
</table>
学生姓名
单位
位置
城市
反恐委员会
债券
{u表%中std的%
{{std.first_name}
{{std.company}}
{{std.position}}
{{std.city}
{{std.ctc}}
{{std.bond}}
{%endfor%}
您尝试做的事情比组合两个查询集更容易实现。由于您有一个OneToOneField用户,您可以直接访问如下用户详细信息:{{{std.student.first_name}}}
在for循环中。现在,若我明白你们想要什么,你们可能需要在Placement表中的Profile表的外键。通过这种方式,您可以访问配置文件{{std.profile.first_name}
,用户表{{std.profile.user.first_name}}
,谢谢。成功了!!。我在Placement中实现了一个新属性,该属性保存Profile表的外键。