Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#24937 closed Bug (fixed)

DateTimeRangeField.value_to_string raises TypeError

Reported by: Villiers Strauss Owned by: Matthew Somerville
Component: contrib.postgres Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

DateTimeRangeField's value_to_string method raises TypeError because json.dumps is unable to serialize datetime.datetime objects.

Steps to replicate:

Step 1:

# myapp/models.py
from django.contrib.postgres.fields import DateTimeRangeField
from django.db.models import Model

class DateTimeRangeTest(Model):
    date_time_range = DateTimeRangeField()

Step 2: migrate etc.

Step 3:

from django.contrib.postgres.fields import DateTimeRangeField
from myapp.models import DateTimeRangeTest

t = DateTimeRangeTest.objects.create(date_time_range=('2015-06-05 16:00:00+0200','2015-06-05 17:00:00+0200'))

dtrf = DateTimeRangeField()
dtrf.attname = 'date_time_range'
dtrf.value_to_string(t)  
# raises: TypeError: datetime.datetime(2015, 6, 5, 14, 00, 00, tzinfo=<UTC>) is not JSON serializable

The value_to_string method is used with the manage.py dumpdata command, so this bug effectively breaks dumpdata for models with DateTimeRangeFields.

Change History (10)

comment:1 by Villiers Strauss, 9 years ago

Type: UncategorizedBug

comment:2 by Claude Paroz, 9 years ago

Triage Stage: UnreviewedAccepted

DateField is serializing its value through isoformat(), so I guess date-based range fields should do something similar.

comment:3 by Matthew Somerville, 9 years ago

Owner: set to Matthew Somerville
Status: newassigned

I'll add a test to show this, and hopefully a patch.

comment:4 by Matthew Somerville, 9 years ago

Has patch: set

PR at https://github.com/django/django/pull/4818 – I used DjangoJSONEncoder on the base value_to_string so that it should hopefully handle both DateRangeField and DateTimeRangeField (the changed test did fail on both those ranges). The PR does pass, the failure is in the last commit on master, I'm afraid :)

comment:5 by Matthew Somerville <matthew-github@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 2926559c:

Fixed #24937 -- fix serialization of Date(Time)RangeField.

Use the DjangoJSONEncoder so that datetime and date are encoded
appropriately.

comment:6 by Marc Tamlyn <marc.tamlyn@…>, 9 years ago

In d58816b:

Merge pull request #4818 from dracos/24937-ranging-to-victory

Fixed #24937 -- fix serialization of Date(Time)RangeField.

comment:7 by Tim Graham <timograham@…>, 8 years ago

In 3ded51bc:

[1.8.x] Fixed #24937 -- Fixed serialization of Date(Time)RangeField.

Use the DjangoJSONEncoder so that datetime and date are encoded
appropriately.

Backport of 2926559cce34e48efb4b073721926d737e372dd3 from master

comment:8 by Tim Graham <timograham@…>, 8 years ago

In 34bb6c0:

Refs #24937 -- Forwardport of 1.8.7 release note.

Forwardport of 3ded51bcf21f384b2e80ce97aff5c12d63e5f0e3 from stable/1.8.x

comment:9 by Tim Graham <timograham@…>, 8 years ago

In 5bb40e7:

[1.9.x] Refs #24937 -- Forwardport of 1.8.7 release note.

Forwardport of 3ded51bcf21f384b2e80ce97aff5c12d63e5f0e3 from stable/1.8.x

comment:10 by Tim Graham <timograham@…>, 8 years ago

In 2c96b3d:

[1.8.x] Refs #24937 -- Backported more commits to fix for serialization of Date(Time)RangeField.

Instead of using DjangoJSONEncoder, use base_field's value_to_string().

Note this means the serialization of e.g. IntegerRangeField now has
strings for lower and upper, so use to_python when they came back in
(same behaviour as ArrayField, hopefully, from where I also got the
set_attributes_from_name function).

Backport of 86d9b10dc33cc115fee2ecab40a569354ac55d15 and
8a842148b6deaab021526e2689279cf5e232945f from master

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