#30820: DateRange__contains does not work as filter
----------------------------------+--------------------------------------
     Reporter:  henhuy            |                    Owner:  (none)
         Type:  Bug               |                   Status:  new
    Component:  contrib.postgres  |                  Version:  2.2
     Severity:  Normal            |               Resolution:
     Keywords:  postgres,         |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Description changed by henhuy:

Old description:

> 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 `get`works instead...

New description:

 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 `get`works instead...

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30820#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.b50b90743700e1c2ec2c6d09db9324ff%40djangoproject.com.

Reply via email to