I have following model:
class MyModel(models.Model):
due = models.DateTimeField(null=True)
max_days_before_due = models.IntegerField()
I would like to filter out instances or rows that are past this due, ie. when due
minus max_days_before_due
is in the past. I am using this query:
current_date = timezone.now()
MyModel.objects.annotate(
counted_days=Case(
When(
due__isnull=False,
then=ExtractDay(F('due')-current_date)
),
default=0,
output_field=IntegerField()
)
).filter(
max_days_before_due__lt=F('counted_days')
)
I am receiving this error:
django_1 | File "/usr/local/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
django_1 | return self.cursor.execute(sql, params)
django_1 | django.db.utils.ProgrammingError: function pg_catalog.timezone(unknown, interval) does not exist
django_1 | LINE 1: ... '2020-08-26T15:03:11.111165+00:00'::timestamptz) AT TIME ZO...
django_1 | ^
django_1 | HINT: No function matches the given name and argument types. You might need to add explicit type casts.
When I delete the subtration from the annotation, the error does not show, but I need to be able to count the timedelta. Otherwise if I set the due
to, let's say, 2020-08-30
and current_date
is 2020-08-26
it returns 30 in the counted_days
field instead of 4.
I've been trying to use this question and these docs as reference. I am using PostgreSQL in version 10 and Django in version 1.11, Python 3.4.
EDIT
It seems that this task should be pretty straightforward using plain SQL:
postgres=# SELECT DATE '2020-08-26' + 10;
?column?
------------
2020-09-05
Maybe there's an easier way of doing this.