Django left join m2m field.

Posted by duder on Stack Overflow See other posts from Stack Overflow or by duder
Published on 2010-05-16T20:30:01Z Indexed on 2010/05/16 21:40 UTC
Read the original article Hit count: 257

Filed under:
|

Here's my Model:

class User(models.Model):
    pass

class Item(models.Model):
    pass

class ItemVote(models.Model):
    user = models.ForeignKey(User)
    item = models.ForeignKey(Item)
    vote = models.BooleanField()

I want to retrieve a list of Items, and I want to know if the current user has voted for each Item. How do I alter my query object so that it will generate sql similar to:

SELECT ...
FROM items
LEFT OUTER JOIN item_votes ON (item_votes.user_id = ? AND
                               item_votes.item_id = items.id)

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models