你好,我在试着让two model inside one view (def) to show data in one html page
我用views.py写的,但它显示'<' not supported between instances of 'Tech' and 'Mobile',我不知道有什么问题
Views.py:
def home(request):
mobileforhome = Mobile.objects.all()
techforhome = Tech.objects.all()
results = list(sorted(chain(mobileforhome,techforhome)))
paginator = Paginator(results,6)
page = request.GET.get('page')
results = paginator.get_page(page)
context = {'results':results}
return render(request,'website_primary_html_pages/home.html',context=context)发布于 2020-09-18 21:12:58
默认情况下,Django模型实例是不可排序的;最简单的解决方案是在sorted()中使用key function来定义顺序。
例如,如果要按两个模型的ID字段排序:
results = list(
sorted(
chain(mobileforhome, techforhome),
key=lambda x: x.id))https://stackoverflow.com/questions/63955820
复制相似问题