[Answered ]-How to traverse over reversed queryset in django?

1👍

reversed(…) [Python-doc] is an iterator, not an iterable, so the second time you loop, the iterator is already "exhausted". It is thus just a "shallow object", that "walks" through the QuerySet in reversed order.

You thus use reversed(…) twice. This will not make the query a second time, since the QuerySet will cache the result, so:

x = Subscription.objects.filter(customer_id=customer_id).order_by('-id')[:count]
tmp = reversed(x)
y = call_function(subs=tmp)

for j in reversed(x):
    print(j)

Leave a comment