Reputation: 91
I wanted to make a query
query = Rating.objects.filter(human=pk)
friendliness = query.aggregate(Avg('friendliness'))
punctuality = query.aggregate(Avg('punctuality'))
knowledge = query.aggregate(Avg('knowledge'))
and show this in my html. Is the best practice to put it in views.py or is it better to put it in models.py?
Upvotes: 0
Views: 109
Reputation: 4512
I think this is best suited for models.Manager
:
class RatingManager(models.Manager):
...
def aggregate_by_friendliness(self, human):
return self.filter(human=human).aggregate(Avg('friendliness'))
class Rating(models.Model):
...
objects = RatingManager()
So you can reuse it like this:
friendliness = Rating.objects.aggregate_by_friendliness(human=pk)
Upvotes: 1