我想列出来自两个不同模型“ Post”和“ Superpost”的帖子 但是我该如何访问呢?
def post_list(request):
list_posts = Post.objects.get_queryset().order_by('-pk')
paginator = Paginator(list_posts, 10) # Show 10 Posts per page
page = request.GET.get('postpage')
posts = paginator.get_page(page)
categories = Category.objects.all()
return render(request, 'MyProject/post_list.html', {'posts': posts, 'categories':categories})
答案 0 :(得分:0)
感谢您的所有答复,我解决了这样的问题:
def post_list(request):
list_posts = list(chain(Post.objects.get_queryset().order_by('-pk'), Post_Sell_Multiple.objects.get_queryset().order_by('-pk')))
paginator = Paginator(list_posts, 10) # Show 10 Posts per page
page = request.GET.get('postpage')
posts = paginator.get_page(page)
categories = Category.objects.all()
return render(request, 'MyProject/post_list.html', {'posts': posts, 'categories':categories})
答案 1 :(得分:-1)
您应该首先查询SuperPost模型,然后像这样链接Post和SuperPost查询集:
from itertools import chain
def post_list(request):
list_posts = Post.objects.get_queryset().order_by('-pk')
list_super_posts = SuperPost.objects.get_queryset().order_by('-pk')
result_list = list(chain(list_posts, list_super_posts))
paginator = Paginator(result_list, 10) # Show 10 Posts per page
page = request.GET.get('postpage')
posts = paginator.get_page(page)
categories = Category.objects.all()
return render(request, 'MyProject/post_list.html', {'posts': posts,
'categories':categories})