I have a utility function in my program for searching for entities. It takes a max_count parameter. It returns a QuerySet.
I would like this function to limit the max number of entries. The standard way would be to take a slice out of my QuerySet:
return results[:max_count]
My problem is that the views which utilize this function sort in various ways by using .order_by(). This causes exceptions as re-ordering is not allowed after taking a slice.
Is it possible to force a “LIMIT 1000” into my SQL query without taking a slice?
Do
results[:max_count]in a view, after.order_by(). Don’t be afraid of requesting too much from DB, query won’t be evaluated until the slice (and even after it either).