#36328: Deserialization of RangeFields not interpreting the range bounds into
Python types
-------------------------------------+-------------------------------------
Reporter: Sebastian Albert | Type: Bug
Status: new | Component:
| contrib.postgres
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage:
range,serialization,postgres | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
I ran into an issue using Django REST Framework on a simple model with a
field of type `DateRangeField`:
When POSTing a new instance to DRFs list view via a JSON body with the
field's key mapped to the value ["2025-04-14", "2025-05-15"]
the new instance is saved, but the immediately returned page is an error,
indicating that `.isoformat()` has been called on an object of type `str`
[1], which is "2025-04-14" in my example. Apparently, the `RangeField`
deserialization [2] did not deserialize the range bounds into the type of
the Range's `base_field` [3]. psycopg seems to handle this without issue,
but as the deserialized object is used in the view to be serialized again,
this doesn't work out.
I am entirely new to Django, but from what I understand, I *think* the
instance of `DateRangeField` should have bounds of type `date` and not
`str`.
Retrieving the successfully saved entity via a subsequent `GET` works
fine, which is in line with that reasoning, as psycopg returns Postgres'
`daterange` bounds as `date` instances.
[1]
https://github.com/django/django/blob/5.2/django/contrib/postgres/fields/ranges.py#L128
[2]
https://github.com/django/django/blob/5.2/django/contrib/postgres/fields/ranges.py#L98-L108
[2]
https://github.com/django/django/blob/5.2/django/contrib/postgres/fields/ranges.py#L204
--
Ticket URL: <
https://code.djangoproject.com/ticket/36328>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.