Maquisard
Maquisard

Reputation: 83

Annotate with a static date - Django

I'm trying to annotate a queryset with a static date in Django.

With an integer (instead of a date) it works:

from django.db.models import Value, IntegerField

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

How can I make it works with date??

from django.db.models import Value, DateField

cars= Car.objects.all().annotate(mydate=Value('2019-01-01', DateField()))

Upvotes: 0

Views: 229

Answers (1)

ac2001
ac2001

Reputation: 736

You can use Cast

cars= Car.objects.annotate(sales=Cast(Value('20190101'), output_field=DateField()))

Upvotes: 1

Related Questions