From de77d99522687e48a1dd76656fbc8302e1d50ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Tue, 11 May 2021 16:13:15 +0200 Subject: [PATCH] misc: fix |count filter for None values (#53924) --- tests/test_formdata.py | 4 ++++ wcs/qommon/templatetags/qommon.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/tests/test_formdata.py b/tests/test_formdata.py index a8d14665d..082adfc86 100644 --- a/tests/test_formdata.py +++ b/tests/test_formdata.py @@ -2565,6 +2565,10 @@ def test_lazy_formdata_count_as_len_filter(pub): tmpl = Template('{{ form_var_value|count }}') assert tmpl.render(context) == '4' + tmpl = Template('{{ form_var_value|count }}') + assert tmpl.render({}) == '0' + assert tmpl.render({'form_var_value': None}) == '0' + def test_rounding_and_abs_conditions_django(pub, variable_test_data): for true_condition_value in ( diff --git a/wcs/qommon/templatetags/qommon.py b/wcs/qommon/templatetags/qommon.py index 45960a1f6..ed5974d8b 100644 --- a/wcs/qommon/templatetags/qommon.py +++ b/wcs/qommon/templatetags/qommon.py @@ -687,6 +687,8 @@ def filter_value(queryset, value): def count(queryset): if hasattr(queryset, 'get_value'): queryset = queryset.get_value() # unlazy + if queryset is None: + return 0 return len(queryset)