Django的多个查询集合并成一个分页 [英] Django multiple queryset combine into a pagination
问题描述
我将来自不同模型的2个查询集组合到一个列表中,并使用分页显示为单个列表。
I have combine 2 queryset from different models into a list and used pagination to display as a single list.
问题是列表中的对象显示为
The problem is the objects from the list are displayed by the pagination according to the models they were created from.
如何修复列表,以便在通过模型创建新对象时使用。即使最近创建的对象来自其他模型,它也会显示在最近创建的对象之后。
How could I fix the list so when a new object is created from the models. It will be displayed after the recently created object even though the recent object created was from a different models.
示例将是。我拥有用户白板和用户评论的组合查询集。如果最近创建了一个新的白板对象,那么我将创建另一个注释。该注释将按分页显示在白板的对象之后,而不是与其他所有注释一起显示在分页后面,因为它在不同的模型中
Example would be . I have a combine queryset of user's whiteboard and user's comment. If a new whiteboard 's object was created recently and I would create another comment . The comment would be displayed by the pagination after the whiteboard 's object instead of been displayed with all the other comments further down the paginated because it was in a different models
< img src = https://i.stack.imgur.com/l2Ajw.jpg alt =在此处输入图片描述>
我希望您可以我要实现的目标的想法:)
I hope you can get the idea of what i'm trying to achieve :)
模块的一部分
class WhiteBoard(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
picture = models.OneToOneField('Picture',related_name='picture',blank=True,null=True)
def __unicode__(self):
return self.name
class Comment(models.Model):
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User)
body = models.TextField()
picture = models.ForeignKey(Picture)
views.py
@login_required
def archive(request):
user = User.objects.get(username=request.user)
person = Person.objects.get(user=user)
users = User.objects.filter(pk__in=person.following.all().values_list('user__pk',flat=True))
picture = whiteboard .objects.filter(user__in=users).order_by("-created")
comment = Comment.objects.filter(user__in=users).order_by("-created")
posts= list(chain(picture,comment))
paginator = Paginator(posts, 5)
try: page = int(request.GET.get("page", '1'))
except ValueError: page = 1
try:
posts = paginator.page(page)
except (InvalidPage, EmptyPage):
posts = paginator.page(paginator.num_pages)
return render(request,'archive.html',{'picture':picture,'comment':comment,'posts':posts})
archive.html
archive.html
{% for post in posts.object_list %}
{{post.name }}
<br>
{{post.body}}
{% endfor %}
<!-- Next/Prev page links -->
{% if posts.object_list and posts.paginator.num_pages > 1 %}
<div class="pagination" style="margin-top: 20px; margin-left: -20px; ">
<span class="step-links">
{% if posts.has_previous %}
<a href= "?page={{ posts.previous_page_number }}">newer entries << </a>
{% endif %}
<span class="current">
Page {{ posts.number }} of {{ posts.paginator.num_pages }}
</span>
{% if posts.has_next %}
<a href="?page={{ posts.next_page_number }}"> >> older entries</a>
{% endif %}
</span>
</div>
{% endif %}
</div>
推荐答案
您只需要通过创建的
属性对组合的查询集列表进行排序他们俩都共享:
You just need to sort your list of combined querysets by the created
attribute they both share:
from itertools import chain
...
posts = list(
sorted(
chain(picture,comment),
key=lambda objects: objects.created,
reverse=True # Optional
))
paginator = Paginator(posts, 5)
...
这篇关于Django的多个查询集合并成一个分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!