1👍
This kind of filtering should be done in the controller instead. You filter the out the non-appropriate candidates in your Query before passing it to the template.
0👍
This should select all candidates that don’t have blocked_user relationships with election in your view.
eligible_candidates = election.candidate_set.exclude(id__in = election.blocked_users.all().values_list('id'))
then in your template
{% for candidate in eligible_candidates %}
<h3>{{ candidate.name }}</h3>
{% endfor %}
Can be useful to move this filtering on to a method on the Model or Model manager if this filtering will be used often (DRY).
- [Answer]-Subclassing django model doesn't carry the Manager
- [Answer]-Can not apply inline styling in Django
- [Answer]-Django Migrate – change charfield to datetimefield
Source:stackexchange.com