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)
- [Answer]-Validate at least One check box is checked in django html template via Ajax
- [Answer]-Switch model according to user
- [Answer]-Python django class based view and functional view
- [Answer]-Passing a Tuple of Values through a Checkbox
Source:stackexchange.com