当我知道x的id时,如何在x对象之前说出前5个对象?
例如,我得到像这样
的模型的最后5个对象numPosts = Post.objects.filter(topic=topic).count()
pre = numPosts - 5
posts = Post.objects.filter(topic=topic).order_by('date')[pre:numPosts]
现在,如果我知道具有最早日期的帖子的ID,我如何获得5个下一个对象,如果没有足够的话,可以更少?
答案 0 :(得分:0)
http://docs.djangoproject.com/en/dev/topics/db/queries/#limiting-querysets
posts = Post.objects.filter(topic=topic).order_by('date')
x = posts.get(id=id)
next_five = posts.filter(date__lt=x.date)[:5]
should return the next 5 (or less) objects