DUPLICATE: Using the Django Custom Model Method Property in order_by ()
I have two models; which stores messages and another that stores votes made on these posts related to using the ForeignKey field. Each vote is stored as a separate record, since I need to track the user and the date and time when the vote was cast.
I created a helper function that counts all votes using the summation function of Django 1.1.
class Post(models.Model):
...some fields...
def tally(self):
return self.vote_set.all().aggregate(Sum('value'))['value__sum'] or 0
class Vote(models.Model):
post = models.ForeignKey(Post)
value = models.IntegerField()
...some fields...
The one query I need to do is to make one value order_byfor the count. But:
Post.objects.all().order_by('tally')
produces the following template error:
: 'tally' . : date_created, , id, is_active, , , slug, user, vote
order_by() ?