Django jinja queryset filter lenth

68 views
Skip to first unread message

N'BE SORO

unread,
Aug 8, 2020, 5:56:48 PM8/8/20
to django...@googlegroups.com
Good evening please

Can you help me?

I want to display the number of items with a filter.
Example:
this call returns 4 elements
{{ item.class.classecours.all|length}}

at the .all level  
make an item.class.classecours.filter(status==True)|length

Agoua David

unread,
Aug 8, 2020, 8:28:46 PM8/8/20
to django...@googlegroups.com
You Can try to use .count instead

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOtSHp_wH9Xy-sf9t%2BMdtWB-bnRX680R9iJwZAnyZXh%3DxV%3De%3Dg%40mail.gmail.com.

Agoua David

unread,
Aug 8, 2020, 8:31:22 PM8/8/20
to django...@googlegroups.com
As Django template engine do not support the parenthèses you Can try to make a custom filter

Liu Zheng

unread,
Aug 9, 2020, 9:13:24 AM8/9/20
to django...@googlegroups.com
Hi,

Not sure it's a good idea to do database query inside the template. Templates are capable of running some logics but for a heavy-lifting job like database query, it should be a good practice to do it in the view, and only pass the results to the template.

N'BE SORO

unread,
Aug 12, 2020, 12:27:29 PM8/12/20
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages