search through related field for specific related object

Posted by dotty on Stack Overflow See other posts from Stack Overflow or by dotty
Published on 2010-06-02T10:19:13Z Indexed on 2010/06/02 10:24 UTC
Read the original article Hit count: 279

Filed under:
|
|
|

Hay, I'm working on a simple voting system, where users can vote on a poll.

My model looks like this

class Poll(models.Model):
    question = models.CharField()
    votes = models.IntegerField()
    usersVoted = models.ManyToManyField(User)

Now, i want to scan through the usersVoted field to see if a User object is in there. This User object will then be matched against a logged in User (User obj saved in a session).

If the User has voted i want to be able to set a 'has_voted' field. I then want to use this field in my view.

I will be using the Property() thingy inside the model to work this 'has_voted' value out.

Like so

    def _can_vote(self):
        return False

    can_vote = property(_can_vote)

Obviously it'll return a Bool.

Any ideas how to search through a related field to find a specific Object?

© Stack Overflow or respective owner

Related posts about django

Related posts about search