[Django]-Efficient Django query filter first n records

2👍

I found the answer myself. Basically the [:n] puts a SQL "LIMIT" in the query which makes the query itself NOT exhaustive. So it’s fine in terms of efficiency.

Link for similar answer below.
https://stackoverflow.com/a/6574137/4775212

0👍

If the limit is known, it can also be using range.

Message.objects.filter(chat__range=[chat_id, chat_id+n]).order_by('-id')

Leave a comment