【发布时间】:2020-10-17 19:40:42
【问题描述】:
我有一个抽象类 Item 和多个继承自 Item 的产品类。在我的模板中,我想要从 Item 继承的每个类的 for 循环,但我不想为从 Item 继承的每个类重写 for 循环。我想要 Item 类的 for 循环,它将给我从 Item 继承的每个子类。
【问题讨论】:
标签: python-3.x django django-models django-views django-templates
我有一个抽象类 Item 和多个继承自 Item 的产品类。在我的模板中,我想要从 Item 继承的每个类的 for 循环,但我不想为从 Item 继承的每个类重写 for 循环。我想要 Item 类的 for 循环,它将给我从 Item 继承的每个子类。
【问题讨论】:
标签: python-3.x django django-models django-views django-templates
# 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)
【讨论】: