djangodjango-templatesmanytomanyfield

is there a way to access a single element of many-to-many relation in django without using for loop?


I'm really new to using Django and I've been stuck on this for weeks. I've got a list of voters which is named thumbs under each candidate, and it is a many-to-many field which is populated once a user votes. How can I check if a user is already in thumbs (i.e. has already voted) so that I display components differently based on this?

Here is what I've got:

myTemplateFile.html

{% if request.user == candidate.thumbs.all %}
    <small style="color:grey; font-size:10px;">you've already voted here</small> 
    <a id="Thumbs" class="btn btn-success btn-sm thumbaction{{poll.id}}{{candidate.id}}"  value="thumbsup" data-toggle="tooltip" title="upVote: I like this candidate" role="button">
        <svg id="{{poll.id}}{{candidate.id}}thumbsup" width="1em" height="1em" viewBox="0 0 16 16" class="up bi bi-hand-thumbs-up d-none"
        xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd"/>
        </svg><span id="i_like_this"><span class="" style="color:white; font-size:xx-small" id="{{poll.id}}{{candidate.id}}up">{{candidate.thumbsup}} </span></span><font class="" style="color:white; font-size:xx-small;" id="{{poll.id}}{{candidate.id}}approve">&nbsp;approved</font> 
    </a>
    <a id="Thumbs" class="btn btn-danger btn-sm thumbaction{{poll.id}}{{candidate.id}}" value="thumbsdown" data-toggle="tooltip" title="downVote: I dont like this candidate" role="button">
        <svg id="{{poll.id}}{{candidate.id}}thumbsdown" width="1em" height="1em" viewBox="0 0 16 16" class="down bi bi-hand-thumbs-down d-none"
        fill="currentColor" xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd"/>
        </svg>
        <span class="" style="color:white; font-size:xx-small;" id="{{poll.id}}{{candidate.id}}down">{{candidate.thumbsdown}} </span><font style="color:white; font-size:xx-small;" id="{{poll.id}}{{candidate.id}}reject">&nbsp;rejected</font>  
    </a>
{% else %}
    <a id="Thumbs" class="btn btn-success btn-sm thumbaction{{poll.id}}{{candidate.id}}"  value="thumbsup" data-toggle="tooltip" title="upVote: I like this candidate" role="button">
        <svg id="{{poll.id}}{{candidate.id}}thumbsup" width="1em" height="1em" viewBox="0 0 16 16" class="up bi bi-hand-thumbs-up"
        xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd"/>
        </svg><span style="color:white; font-size:xx-small;" id="i_like_this"><span class="d-none" style="color:white; font-size:xx-small;" id="{{poll.id}}{{candidate.id}}up">{{candidate.thumbsup}} </span></span><font class="" style="color:white; font-size:xx-small;" id="{{poll.id}}{{candidate.id}}approve">&nbsp;approve</font> 
    </a>
    <a id="Thumbs" class="btn btn-danger btn-sm thumbaction{{poll.id}}{{candidate.id}}" value="thumbsdown" data-toggle="tooltip" title="downVote: I dont like this candidate" role="button">
        <svg id="{{poll.id}}{{candidate.id}}thumbsdown" width="1em" height="1em" viewBox="0 0 16 16" class="down bi bi-hand-thumbs-down"
        fill="currentColor" xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd"/>
        </svg>
        <span style="color:white; font-size:xx-small;" class="d-none" id="{{poll.id}}{{candidate.id}}down">{{candidate.thumbsdown}} </span><font class"" style="color:white; font-size:xx-small;" id="{{poll.id}}{{candidate.id}}reject">&nbsp;reject</font>  
    </a>
{% endif %}

myModelFile.py

class PollCandidate(models.Model):
    name = models.CharField(max_length=500, blank=True, null=True)
    candidate = models.ForeignKey(Candidate, blank=True, null=True, on_delete=models.SET_NULL)
    poll = models.ForeignKey(Polls, on_delete=models.SET_NULL, blank=True, null=True, default=None)
    points = models.IntegerField(default='1')
    thumbsup = models.IntegerField(default='0')
    thumbsdown = models.IntegerField(default='0')
    thumbs = models.ManyToManyField(User, related_name='pollthumbs', default=None, blank=True)

    def __str__(self):
        return f"{self.name}, in {self.poll} "
    
class Votes(models.Model):
    candidate = models.ForeignKey(PollCandidate, related_name='candidateid', on_delete=models.CASCADE, default=None, blank=True)
    poll = models.ForeignKey(Polls, related_name='pollid', on_delete=models.CASCADE)
    user = models.ForeignKey(User, related_name='voterid',on_delete=models.CASCADE, default=None, blank=True)
    vote = models.BooleanField(default=True)

     def __str__(self):
        return f"{self.user} voted for {self.candidate.name} in {self.poll}"

I'll appreciate every suggestion 😊


Solution

  • Change the check in the first line of your template to:

    {% if request.user in candidate.thumbs.all %}