Opened 5 years ago

Closed 5 years ago

#30820 closed Bug (worksforme)

DateRange__contains does not work as filter.

Reported by: henhuy Owned by:
Component: contrib.postgres Version: 2.2
Severity: Normal Keywords: postgres,
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by henhuy)

Having a model containing django.contrib.postgres.fields.DateRangeField I cannot filter the range by date using "contains", BUT I can get one object using "contains".

Example (my original case, is different - but this should show the error as well):

import datetime as dt
from django import models
from django.contrib.postgres.fields import DateRangeField

class Period(models.Model):
    value = models.IntegerField()
    period = DateRangeField()

date = dt.date(2017, 1, 1)

# This works:
period = Period.objects.get(period__contains=date)

# This does not work:
period = Period.objects.filter(period__contains=date).first()

SQL-Query:
SELECT "period"."id", "period"."value", "period"."period" FROM "period" WHERE "period"."period" @> (2017-01-01)::date
SQL-Error (translated from german):
FEHLER: Cannot transform Typ integer into date

There should be ' around the date, then it would work:
SELECT "period"."id", "period"."value", "period"."period" FROM "period" WHERE "period"."period" @> ('2017-01-01')::date

Don't know why getworks instead...

Change History (2)

comment:1 by henhuy, 5 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: worksforme
Status: newclosed
Summary: DateRange__contains does not work as filterDateRange__contains does not work as filter.

Thanks for this report, however I cannot reproduce this issue in Django 2.2, 3.0 or on master. Everything works for me.

Note: See TracTickets for help on using tickets.
Back to Top