A QuerySet by aggregate field value

后端 未结 3 452
梦如初夏
梦如初夏 2020-11-27 04:02

Let\'s say I have the following model:

class Contest:
    title = models.CharField( max_length = 200 )
    description = models.TextField()

class Image:
            


        
3条回答
  •  情深已故
    2020-11-27 04:21

    Oh, of course I forget about new aggregation support in Django and its annotate functionality.

    So query may look like this:

    Contest.objects.get(pk=id).image_set.annotate(score=Sum('vote__value')).order_by( 'score' )
    

提交回复
热议问题