社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

如何迭代两个django模型

iri0021 • 3 年前 • 1394 次点击  

我的django项目中有多个模型。我想迭代它们,以便在django模型字段中访问它们。我该怎么做?

我试着像我的观点那样压缩它们。比如:

inventory = Inventory.objects.all()
header = Header.objects.all()
setup = Setup.objects.all()
revision = Revisions.objects.all()
spec = SpecificationDetails.objects.all()
review = Reviewers.objects.all()

zippedItems = zip(inventory, header, setup, revision, spec, review)
context = {
        'zippedItems':  zippedItems

}
return render(request, 'crud/dashboard.html', context)

在我的模板文件中,我尝试了

{%for items in zippedItems %}
<td>{{items}}</td>
{% endfor %}

但它不起作用,我认为它没有把物体拉上拉链。关于如何做到这一点,有什么建议吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130260
 
1394 次点击  
文章 [ 3 ]  |  最新文章 3 年前
Dharman CPSuperstore
Reply   •   1 楼
Dharman CPSuperstore    3 年前

请像这样的用户转换queryset模型到列表,然后添加到列表中。。。所有列表的类型是元组列表。 然后 您必须将列表转换为zip。。 将此zip文件传递到数据帧。这将自动管理zip文件,以正确的列格式提供数据。。

inventory = list(Inventory.objects.all())
header = list(Header.objects.all())
setup = list(Setup.objects.all())
revision = list(Revisions.objects.all())
spec = list(SpecificationDetails.objects.all())
review = list(Reviewers.objects.all())
iterates = inventory + header + setup + revision + spec + review

zip_file = list(zip(iterates))

import pandas as pd

df = pd.DataFrame(zip_file, columns = ['Inventory', 'Header','Setup','Revisoin','Spec','Review'])

context = {
        'df':  df
}
return render(request, 'crud/dashboard.html', df)
Jack
Reply   •   2 楼
Jack    3 年前

我想你可以先列出queryset,然后把它们组合在一起。

inventory = list(Inventory.objects.all())
header = list(Header.objects.all())
setup = list(Setup.objects.all())
revision = list(Revisions.objects.all())
spec = list(SpecificationDetails.objects.all())
review = list(Reviewers.objects.all())
iters = inventory + header + setup + revision + spec + review
Sabil
Reply   •   3 楼
Sabil    3 年前

我假设你有相似数量的元素 清单、标题、设置、修订、规格和审查 .请记住,如果元素的数量不同,那么它将至少计算一个。

请在下面查看 zip 为更好的理解树立榜样。

ZIP示例:

inventory = [1, 2, 3, 4]
header = [5, 6, 7, 8]
setup = [9, 10, 11, 12]
revision = [13, 14, 15, 16]
spec = [17, 18, 19, 20]
review = [21, 22, 23, 24, 25, 26]

zippedItems = zip(inventory, header, setup, revision, spec, review)

for inventory, header, setup, revision, spec, review in zippedItems:
    print(f'inventory: {inventory} - header: {header} - setup: {setup} - revision: {revision} - spec: {spec} - review: {review}')

输出:

inventory: 1 - header: 5 - setup: 9 - revision: 13 - spec: 17 - review: 21
inventory: 2 - header: 6 - setup: 10 - revision: 14 - spec: 18 - review: 22
inventory: 3 - header: 7 - setup: 11 - revision: 15 - spec: 19 - review: 23
inventory: 4 - header: 8 - setup: 12 - revision: 16 - spec: 20 - review: 24

从输出中可以看到,它没有打印审阅列表的最后一个值,因为这些值不在4的范围内。

您可以保持此代码的原样:

inventory = Inventory.objects.all()
header = Header.objects.all()
setup = Setup.objects.all()
revision = Revisions.objects.all()
spec = SpecificationDetails.objects.all()
review = Reviewers.objects.all()

zippedItems = zip(inventory, header, setup, revision, spec, review)
context = {
        'zippedItems':  zippedItems

}
return render(request, 'crud/dashboard.html', context)

你对HTML部分有问题。应该是这样的:

{% for inventory, header, setup, revision, spec, review in zippedItems %}
    {{ forloop.counter }}
    {{ inventory.model_field_name }}
    {{ header.model_field_name }}
    {{ setup.model_field_name }}
    {{ revision.model_field_name }}
    {{ spec.model_field_name }}
    {{ review.model_field_name }}
{% endfor %}

如果您仍然面临任何问题,请告诉我。:)