A QuerySet by aggregate field value

后端 未结 3 461
梦如初夏
梦如初夏 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:20

    You can write your own sort in Python very simply.

    def getScore( anObject ):
        return anObject.score()
    objects= list(Contest.objects.get( pk = id ).image_set)
    objects.sort( key=getScore )
    

    This works nicely because we sorted the list, which we're going to provide to the template.

提交回复
热议问题