[Django] #36328: Deserialization of RangeFields not interpreting the range bounds into Python types

10 views
Skip to first unread message

Django

unread,
Apr 14, 2025, 1:45:28 PM4/14/25
to django-...@googlegroups.com
#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.

Django

unread,
Apr 14, 2025, 2:57:06 PM4/14/25
to django-...@googlegroups.com
#36328: Deserialization of RangeFields not interpreting the range bounds into
Python types
-------------------------------------+-------------------------------------
Reporter: Sebastian Albert | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 5.2
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage:
range,serialization,postgres | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* easy: 1 => 0
* resolution: => needsinfo
* status: new => closed

Comment:

Hello Sebastian, thank you for taking the time to create this report. In
order to continue this ticket triage, we would need a way to reproduce the
issue using Django without requiring any other dependency. Ideally, could
you provide a test case for the Django codebase showcasing the issue and
the expected outcome? Or, a minimal Django test project and detailed
instructions to reproduce would also help.

Please feel free to reopen when a reproducer is provided for us to
continue the triaging process. Cheers!
--
Ticket URL: <https://code.djangoproject.com/ticket/36328#comment:1>
Reply all
Reply to author
Forward
0 new messages