Let\'s say I have the following model:
class Contest: title = models.CharField( max_length = 200 ) description = models.TextField() class Image:
Oh, of course I forget about new aggregation support in Django and its annotate functionality.
annotate
So query may look like this:
Contest.objects.get(pk=id).image_set.annotate(score=Sum('vote__value')).order_by( 'score' )