[Answer]-Make a query that hits the DB and then make others from it without hitting

1👍

If you don’t want to hit the database again, and you’re sure that all your data is included in the first query, you’ll need to do the filtering in pure Python.

sub_query = [obj for obj in big_query if obj.something = another_something]

and so on. Note that this returns a list rather than a queryset, and you won’t be able to use any of the standard queryset methods like sort; again, you’d need to do that with the standard Python sorting methods.

0👍

if you havenot read yet about Queryset evaluation:

Template

you can use with to cache the queryset and loop over it without hitting the db.

{% with cached_queryset=object.related_name.all %}
   {% for object in cached_queryset %}
      {# no db hit #}
      todo
   {% endfor %}
{% endwith %}

Views

you can use generators, it is just awesome, this is priceless tutorial

sub_query = [obj for obj in big_query if obj.something == another_something]

(copied from Daniel)

Leave a comment