#34641: Null value referenced from a JSONField using an expression that defines
an
output_field returns the string "null" instead of a None value
---------------------------------+--------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Simon Charette):
> I wouldn't ever expect an ORM to implicitly convert a null value to the
string "null"
It doesn't convert it, that's the thing; it's just how the database
adapter you are using is returning it straight from the database in its
encoded JSON form.
> it works differently depending on what the JSON structure looks like: if
the key doesn't exist at all, everything works as expected, but it breaks
if there's an actual defined value of null
I would argue that it's an eventuality you should build your application
to be resilient against. Without a schema attached to your `JSONField`
(something that Django doesn't support) there is no way for the ORM to
know that you've strictly stashed nullable integers in your `test` key.
Without an explicit `output_field` the inferred one should be `JSONField`
which
[https://github.com/django/django/blob/ee36e101e8f8c0acde4bb148b738ab7034e902a0/django/db/models/fields/json.py#L86C9-L96
has a method that would have unserialized] the string properly (at least
on Django 4.2+). It's the very misuse of `output_field` that appears to
the bug here.
--
Ticket URL: <https://code.djangoproject.com/ticket/34641#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701889b620116-8adeab45-0aa7-4684-b061-78dfef0bb3c8-000000%40eu-central-1.amazonses.com.