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-13T20:00:32Z Indexed on 2010/05/13 20:04 UTC
Read the original article Hit count: 235

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

def hot(request):
    links = Link.objects.select_related().annotate(votes=Count('vote')).order_by('-created')
    for link in links:
        delta_in_hours = (int(datetime.now().strftime("%s")) - int(link.created.strftime("%s"))) / 3600
        link.popularity = ((link.votes - 1) / (delta_in_hours + 2)**1.5)
        if request.user.is_authenticated():
            try:
                link.voted = Vote.objects.get(link=link, user=request.user)
            except Vote.DoesNotExist:
                link.voted = None

    links = sorted(links, key=lambda x: x.popularity, reverse=True)

    links = paginate(request, links, 15)

    return direct_to_template(
        request,
        template = 'links/link_list.html',
        extra_context = {
            'links': links,
        })

The above view actually accomplishes what I need but in what I believe to be a horribly inefficient way. This causes the dreaded n+1 queries, as it stands that's 33 queries for a page containing just 29 links while originally I got away with just 4 queries. I would really prefer to do this using Django's ORM or at least .extra().

Any advice?

© Stack Overflow or respective owner

Related posts about django

Related posts about python