Django multiple queryset combine into a pagination

前端 未结 1 1828
名媛妹妹
名媛妹妹 2021-01-06 17:20

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 b

相关标签:
1条回答
  • 2021-01-06 18:02

    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)
    ...
    

    Here's a similar question on the topic

    0 讨论(0)
提交回复
热议问题