django - ordering queryset by a calculated field

后端 未结 6 699
故里飘歌
故里飘歌 2020-11-30 04:22

I want to have a model with calculated fields that I can apply sorting on. For example, let\'s say that I have the following model:

class Foo(models.Model):
         


        
6条回答
  •  迷失自我
    2020-11-30 04:36

    If you would not mind some logic duplicaton, then the following will work:

    Foo.objects.extra(select={'d_field': 'A - B'}).extra(order_by=['d_field'])
    

提交回复
热议问题