[Django]-Django static annotation

16👍

Update

This solution uses soon-to-be-deprecated API. See this answer for a better way to solve this.

Original Answer

You can use the extra() method. Like this:

Car.objects.all().extra(select = {'sales': 0})

133👍

Django features Value expressions:

from django.db.models import Value

cars= Car.objects.annotate(sales=Value(0))

Prior to Django 3.2, specify the field class:

from django.db.models import Value, IntegerField

cars= Car.objects.annotate(sales=Value(0, IntegerField())) 

Instead of IntegerField() you can use instances of all available model fields (ex: CharField(), …)

Leave a comment