[Solved]-Is it possible to force queryset evaluation while keeping it a queryset


How about just doing a len(myqueryset).

or just doing an conditional check can also force evaluate it.


You can’t keep queryset object when you force it. But you can do something like this:

In : list(Project.objects.values_list('owner', 'owner__email'))
Out: [(1, 'admin@example.com')]

So, use values_list to keep data that you’ll use.



You can easily force an evaluation by iterating the queryset: as long as you ignore the result of the iteration, your object stays a queryset.

for item in myqueryset:

However this won’t really do what you want, since you say you want to call further filter methods on the evaluated queryset. This can’t work, since filter() involves modifying the underlying query, and will always hit the database.

I don’t know why you think you’ll get a performance hit if you skip evaluating the queryset though: the opposite is the case, your proposed evaluation is the unnecessary step.

Leave a comment