django : How to write alias in queryset

后端 未结 5 1801
甜味超标
甜味超标 2021-02-06 04:33

How can one write an alias for the column name in django query set. Would be useful for union-style combinations of two linked field to the same foreign model (for instance).

5条回答
  •  广开言路
    2021-02-06 04:51

    You can annotate the fields as you want, with the F expression:

    from django.db.models import F
    
    models.Table.objects.all().values('m', 'b').annotate(n=F('m'), a=F('b'))
    

提交回复
热议问题