Django: Determining if a user has voted or not

Posted by TheLizardKing on Stack Overflow See other posts from Stack Overflow or by TheLizardKing
Published on 2010-05-12T04:17:01Z Indexed on 2010/05/12 4:24 UTC
Read the original article Hit count: 294

Filed under:
|
|

I have a long list of links that I spit out using the below code, total votes, submitted by, the usual stuff but I am not 100% on how to determine if the currently logged in user has voted on a link or not. I know how to do this from within my view but do I need to alter my below view code or can I make use of the way templates work to determine it?

I have read http://stackoverflow.com/questions/1528583/django-vote-up-down-method but I don't quite understand what's going on ( and don't need any ofjavascriptery).

Models (snippet):

class Link(models.Model):
    category = models.ForeignKey(Category, blank=False, default=1)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    url = models.URLField(max_length=1024, unique=True, verify_exists=True)
    name = models.CharField(max_length=512)

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.url)

class Vote(models.Model):
    link = models.ForeignKey(Link)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u'%s vote for %s' % (self.user, self.link)

Views (snippet):

links = Link.objects.select_related().annotate(votes=Count('vote')).order_by('-created')

© Stack Overflow or respective owner

Related posts about django

Related posts about python