File tree Expand file tree Collapse file tree 2 files changed +21
-11
lines changed Expand file tree Collapse file tree 2 files changed +21
-11
lines changed Original file line number Diff line number Diff line change
1
+ {% load is_empty from ext_theme_tags %}
1
2
{% comment "rst" %}
2
3
Shared list base
3
4
================
92
93
{% comment %}
93
94
We usually pass in a queryset here, but some views emit arrays of objects
94
95
instead, so we support these as well. Therefore, the check here needs to be
95
- for an empty array or an empty queryset. Because Django templates aim to
96
- be as difficult as possible to use, this conditional looks pretty silly.
96
+ for an empty array or an empty queryset.
97
97
98
- This cannot be a simple `if not objects` as this executes the query, which
99
- can cause the view to timeout when the query is very large. See ``bool``
100
- evaluation in:
98
+ We avoid using a simple `if not objects` check, as that would evaluate
99
+ the queryset, which can cause a timeout if the queryset is very large,
100
+ instead we use a custom template tag that call ``exists`` if the value is
101
+ a queryset, or ``not value`` otherwise. See ``bool`` evaluation in:
101
102
102
103
https://docs.djangoproject.com/en/4.2/ref/models/querysets/
103
-
104
- Order on the conditional matters here, `and` has higher precedence. Django
105
- templates don't support parenthesis in conditionals.
106
-
107
- https://docs.djangoproject.com/en/5.0/ref/templates/builtins/#boolean-operators
108
104
{% endcomment %}
109
- {% if objects.exists is None and objects|length == 0 or objects.exists == False %}
105
+ {% if objects|is_empty %}
110
106
{% comment "rst" %}
111
107
112
108
.. _api-template-list-placeholder:
Original file line number Diff line number Diff line change 1
1
import logging
2
2
from urllib .parse import urljoin
3
3
4
+ from django .db .models .query import QuerySet
5
+
4
6
from django import template
5
7
from django .conf import settings
6
8
from django .templatetags .i18n import (
@@ -211,3 +213,15 @@ def readthedocs_language_name_local(lang_code):
211
213
except Exception :
212
214
log .exception ("Error getting language name" )
213
215
return lang_code
216
+
217
+
218
+ @register .filter
219
+ def is_empty (value ):
220
+ """
221
+ Check if an iterable or queryset is empty.
222
+
223
+ This avoids using `not value` on a queryset, so the queryset is not evaluated.
224
+ """
225
+ if isinstance (value , QuerySet ):
226
+ return not value .exists ()
227
+ return not value
You can’t perform that action at this time.
0 commit comments