4👍
✅
You’re saving a queryset
object in the session. Querysets are like SQL statements, but they cache the results. You haven’t run the query when you put it in the session, so what you’re storing is essentially just the query. When it gets pulled out it’s still just a query that hasn’t run, so the queryset gets run again. To ensure that you’re storing just the actual results, do this:
request.session['results_list'] = list(results_list)
and to save you another query when you find count
you can…
request.session['count'] = len(request.session['results_list'])
Also keep in mind that session data is (by default) saved in the database, so you might not be doing yourself any favors by storing a python pickled representation of the full data. It might in fact be faster just to go to the original table and pull it out that way.
Source:stackexchange.com