Santhosh
Santhosh

Reputation: 11834

django queryset: how to get items after a particular id in the queryset

I have a list of posts with id:

I want to sort all the posts based on publish_date and get all the posts after an id.

The following gives me queryset of all posts ordered in publish_date

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

After this how to get queryset with posts after a given id of post.

The ids are not in order. The posts are sorted based on published_date. So the ids can be 10, 12, 5, 7, 2, 32 etc as per published_date order . So if i want the posts after id 5 it should return queryset with ids order 7, 2, 32 only

Upvotes: 2

Views: 2601

Answers (1)

Alasdair
Alasdair

Reputation: 309099

To get all articles with publish_date greater than a particular date, you can use __gt:

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

If you only want the ids, then you can use values_list().

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

Upvotes: 2

Related Questions