django static annotation

后端 未结 2 1408
走了就别回头了
走了就别回头了 2021-02-02 07:55

I want to add a static value to the results of a database query using django (so not using \'raw\' SQL)

For example, if I have an object Car with fields make, model, and

2条回答
  •  暖寄归人
    2021-02-02 08:22

    As of Django 1.8, annotate features Value expression:

    from django.db.models import Value, IntegerField
    
    cars= Car.objects.all().annotate(sales=Value(0, IntegerField()))
    

    Instead of IntegerField you can use all available db fields classes.

提交回复
热议问题