#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 , 10 years ago
| Type: | Uncategorized → Bug |
|---|
comment:2 by , 10 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 10 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
I'll add a test to show this, and hopefully a patch.
comment:4 by , 10 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 :)
DateFieldis serializing its value throughisoformat(), so I guess date-based range fields should do something similar.