[Answer]-How do I prevent blocked users from appearing in this list of users?

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).

👤GCrem

Leave a comment