Opened 7 months ago
Closed 7 months ago
#36328 closed Bug (needsinfo)
Deserialization of RangeFields not interpreting the range bounds into Python types
| Reported by: | Sebastian Albert | Owned by: | |
|---|---|---|---|
| Component: | contrib.postgres | Version: | 5.2 |
| Severity: | Normal | Keywords: | range, serialization, postgres |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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
Change History (1)
comment:1 by , 7 months ago
| Easy pickings: | unset |
|---|---|
| Resolution: | → needsinfo |
| Status: | new → closed |
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!