How to add additional column to Django QuerySet

前端 未结 2 1440
死守一世寂寞
死守一世寂寞 2021-02-07 08:39

I have a QuerySet with Books and I would like to add a score field to every Book result.

qs = Book.objects.all()

In raw SQL I woul

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 09:01

    Raw SQL is not the only way. You can use a Value() expression (see docs here):

    from django.db.models import CharField, Value
    
    MyModel.objects.all().annotate(mycolumn=Value('xxx', output_field=CharField()))
    

提交回复
热议问题