templatetags: make count an alias of length (#49889)
This commit is contained in:
parent
1db7d01f40
commit
b2ba8a118d
|
@ -1157,6 +1157,8 @@ def test_lazy_formdata_queryset_filter(pub, variable_test_data):
|
|||
context = pub.substitutions.get_context_variables(mode='lazy')
|
||||
tmpl = Template('{{form_objects|filter_by:"foo_foo"|filter_value:"bar"|count}}')
|
||||
assert tmpl.render(context) == '7'
|
||||
tmpl = Template('{{form_objects|filter_by:"foo_foo"|filter_value:"bar"|length}}')
|
||||
assert tmpl.render(context) == '7'
|
||||
|
||||
pub.substitutions.feed(formdata)
|
||||
tmpl = Template('{{form_objects|filter_by:"foo_foo"|filter_value:form_var_foo_foo|count}}')
|
||||
|
@ -2291,6 +2293,24 @@ def test_lazy_formdata_ceil_filter(pub):
|
|||
assert tmpl.render(context) == '3.14'
|
||||
|
||||
|
||||
def test_lazy_formdata_count_as_len_filter(pub):
|
||||
formdef = FormDef()
|
||||
formdef.name = 'foobar'
|
||||
formdef.url_name = 'foobar'
|
||||
formdef.fields = [
|
||||
fields.StringField(id='0', label='value', varname='value'),
|
||||
]
|
||||
formdef.store()
|
||||
formdata = formdef.data_class()()
|
||||
formdata.data = {'0': 'coin'}
|
||||
formdata.store()
|
||||
pub.substitutions.feed(formdata)
|
||||
for mode in (None, 'lazy'):
|
||||
context = pub.substitutions.get_context_variables(mode=mode)
|
||||
tmpl = Template('{{ form_var_value|count }}')
|
||||
assert tmpl.render(context) == '4'
|
||||
|
||||
|
||||
def test_rounding_and_abs_conditions_django(pub, variable_test_data):
|
||||
for true_condition_value in (
|
||||
# reminder
|
||||
|
|
|
@ -670,7 +670,9 @@ def filter_value(queryset, value):
|
|||
|
||||
@register.filter
|
||||
def count(queryset):
|
||||
return queryset.count
|
||||
if hasattr(queryset, 'get_value'):
|
||||
queryset = queryset.get_value() # unlazy
|
||||
return len(queryset)
|
||||
|
||||
|
||||
@register.filter
|
||||
|
|
Loading…
Reference in New Issue