I am writing a webapp similar to Stackoverflow. How can I query Questions and annotate each question with its score, which is simply how many upvotes it’s has minus how many downvotes it’s had.
class Question(models.Model):
pass
class Answer(models.Model):
pass
VOTE_CHOICES = (
('U', 'Up'),
('D', 'Down'),
)
class Vote(models.Model):
user = models.ForeignKey(User)
answer = models.ForeignKey(Answer)
type = models.CharField(max_length=1, choices=VOTE_CHOICES, db_index=True)
class Meta:
unique_together = (("user", "answer"),)
will make it much easier: