'''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
`DateTimeRangeField`s.
--
Ticket URL: <https://code.djangoproject.com/ticket/24937>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* type: Uncategorized => Bug
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:1>
* stage: Unreviewed => Accepted
Comment:
`DateField` is serializing its value through `isoformat()`, so I guess
date-based range fields should do something similar.
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:2>
* owner: => dracos
* status: new => assigned
Comment:
I'll add a test to show this, and hopefully a patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:3>
* has_patch: 0 => 1
Comment:
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 :)
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"2926559cce34e48efb4b073721926d737e372dd3" 2926559c]:
{{{
#!CommitTicketReference repository=""
revision="2926559cce34e48efb4b073721926d737e372dd3"
Fixed #24937 -- fix serialization of Date(Time)RangeField.
Use the DjangoJSONEncoder so that datetime and date are encoded
appropriately.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:5>
Comment (by Marc Tamlyn <marc.tamlyn@…>):
In [changeset:"d58816bd6bf93a878085d35d3ed4609efeb0acfa" d58816b]:
{{{
#!CommitTicketReference repository=""
revision="d58816bd6bf93a878085d35d3ed4609efeb0acfa"
Merge pull request #4818 from dracos/24937-ranging-to-victory
Fixed #24937 -- fix serialization of Date(Time)RangeField.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:6>
Comment (by Tim Graham <timograham@…>):
In [changeset:"3ded51bcf21f384b2e80ce97aff5c12d63e5f0e3" 3ded51bc]:
{{{
#!CommitTicketReference repository=""
revision="3ded51bcf21f384b2e80ce97aff5c12d63e5f0e3"
[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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"34bb6c0cb7b5b7b86390252389cc1b2b037d9ec6" 34bb6c0]:
{{{
#!CommitTicketReference repository=""
revision="34bb6c0cb7b5b7b86390252389cc1b2b037d9ec6"
Refs #24937 -- Forwardport of 1.8.7 release note.
Forwardport of 3ded51bcf21f384b2e80ce97aff5c12d63e5f0e3 from stable/1.8.x
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:8>
Comment (by Tim Graham <timograham@…>):
In [changeset:"5bb40e7608c45b35af5868d190a44462ec88d2c0" 5bb40e7]:
{{{
#!CommitTicketReference repository=""
revision="5bb40e7608c45b35af5868d190a44462ec88d2c0"
[1.9.x] Refs #24937 -- Forwardport of 1.8.7 release note.
Forwardport of 3ded51bcf21f384b2e80ce97aff5c12d63e5f0e3 from stable/1.8.x
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:9>
Comment (by Tim Graham <timograham@…>):
In [changeset:"2c96b3da6f9be3795bd2ded59175332aa986bf2d" 2c96b3d]:
{{{
#!CommitTicketReference repository=""
revision="2c96b3da6f9be3795bd2ded59175332aa986bf2d"
[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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24937#comment:10>