1👍
Something that’s important to keep in mind here is the performance implication of having to query a many-to-many relationship for each comparison, or even pre-loading all of the m2m data to see if the user is in the votes_received
queryset.
In a case like this, I usually opt for a de-normalized way to do the boolean comparison. I will create a field to just hold the IDs as comma-separated ints and update the field via a post_save signal.
This greatly simplifies things at the view and template level and also avoids having to do any joins, or any additional queries at all.
Assuming you’re looping over a list of QuoteIdea
instances and passing an instance of CofifiUser
to the template, and you have added a field to QuoteIdea
called something like cofifi_vote_ids
you can do:
{# returns added to prevent wrapping #}
{% for quote_idea in quote_ideas %}
<button class="btn
{% if cofifi_user.id in quote_idea.cofifi_vote_ids %}
disabled
{% else %}
btn-primary
{% endif %}">Button</button>
{% endfor %}
0👍
I did something like this and it worked very good.
<button class="btn btn-primary btn-xs
{% for creator in item.votes_received.all %}
{%if user.id == creator.id %}
disabled
{%endif%}
{% endfor %}
">Send Coffee</button>
- [Answer]-Permission denied on trying to write file
- [Answer]-DELETEing a resource in tastypie
- [Answer]-Django show "It worked" page instead of my project when I start it docker