python-3.x - 如何在 Django 模板中迭代抽象类的对象?
问题描述
我有一个抽象类 Item 和多个继承自 Item 的产品类。在我的模板中,我想要从 Item 继承的每个类的 for 循环,但我不想为从 Item 继承的每个类重写 for 循环。我想要类 Item 的 for 循环,它将给我从 Item 继承的每个子类。
解决方案
一种方法,您可以通过 itertools.chain 合并所有查询集
# views.py
import itertools
def handle(request):
all_products = itertools.chain(qset_a, qset_b, qset_c)
context = {'all_products':all_products}
return render(request, 'template.html', context)
推荐阅读
- java - 考虑定义一个 EntityManagerFactory Spring boot 类型的 bean
- python - 需要一种算法来将命题逻辑表达式从中缀转换为后缀
- bazel - 如何在 starlark 中实现 local_archive()
- jquery - 在 AJAX 调用之后通过 JSON 对象进行迭代
- javascript - 复杂对象中的javascript自引用
- reactjs - axios 结合 url api 为什么返回 promise 不值?
- python - 过滤/插值 x,y 点到特定的点数,并包含要包含的特定点的列表
- java - 使用多页方法(页面对象)进行测试 - Selenium Webdriver
- html - Bootstrap 4 轮播,显示 3. Advance 1
- java - 如何实现休眠