gpt4 book ai didi

python - django 查询集 : how to get items after a particular id in the queryset

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:18 24 4
gpt4 key购买 nike

我有一个 id 为的帖子列表:

我想根据 publish_date 对所有帖子进行排序,并在一个 id 之后获取所有帖子。

下面给出了在 publish_date 中排序的所有帖子的查询集

articles = Article.objects.all().order_by('-publish_date')

在此之后如何在给定的帖子 ID 之后获取包含帖子的查询集。

ID 没有按顺序排列。帖子根据 published_date 排序。因此,根据 published_date order,ID 可以是 10、12、5、7、2、32 等。所以如果我想要 id 5 之后的帖子,它应该只返回 id 顺序为 7、2、32 的查询集

最佳答案

要获取所有 publish_date 大于特定日期的文章,您可以使用 __gt :

article = Articles.objects.get(id=5)
articles = Articles.objects.filter(publish_date__gt=article.publish_date)

如果您只需要 ID,则可以使用 values_list()

ids = articles.values_list('pk', flat=True)

关于python - django 查询集 : how to get items after a particular id in the queryset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com