0

I need only 5th and 30th record (from top) in the below query set?

qs=Posts.objects.filter().orderby('-date')

is there any rownum functionality available in django to achieve this in a single query like rownum=5 ??.

Note: I am using Postgres

  • 2
    Have you checked this: [Django - limiting query results](https://stackoverflow.com/questions/6574003/django-limiting-query-results) or [Limiting Querysets](https://docs.djangoproject.com/en/3.2/topics/db/queries/#limiting-querysets). Using: [start:end]. `qs=Posts.objects.filter().orderby('-date')[5:30]` – Mahrus Khomaini Jul 12 '21 at 11:24

1 Answers1

1

Typecast the queryset into a list then perform normal list indexing to get the desired results. Just use

q = list(qs)

However this is not efficient as it may eat some memory depending on the size of the query set, so other methods you may refer - How to convert a Django QuerySet to a list

cshelly
  • 535
  • 3
  • 10