Re: [Django] #35071: Resolve lazy objects inside dictionaries when saving JSONFields

18 views
Skip to first unread message

Django

unread,
Dec 31, 2023, 11:35:14 AM12/31/23
to django-...@googlegroups.com
#35071: Resolve lazy objects inside dictionaries when saving JSONFields
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jacob Walls):

So I have something sort of ready to go here, but then I noticed that what
I should probably be doing is using `DjangoJSONEncoder` in the `encoder`
arg of `JSONField`, which solves my use case. Is that enough? Should that
be the default?

--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 31, 2023, 11:40:02 AM12/31/23
to django-...@googlegroups.com
#35071: Resolve lazy objects inside dictionaries when saving JSONFields
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jacob Walls):

I guess it would still be nice to separate this concern (resolving the
promise) from the choice of serializer.

--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:9>

Django

unread,
Dec 31, 2023, 12:04:32 PM12/31/23
to django-...@googlegroups.com
#35071: Resolve lazy objects inside dictionaries when saving JSONFields
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/17668 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:10>

Django

unread,
Jan 1, 2024, 2:15:33 PM1/1/24
to django-...@googlegroups.com
#35071: Resolve lazy objects inside dictionaries when saving JSONFields
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

I think using the `encoder` arg of `JSONField` is the right answer to this
issue. The `get_prep_value` of your suggested patch would add too much
overhead in my opinion.

--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:11>

Django

unread,
Jan 1, 2024, 2:48:09 PM1/1/24
to django-...@googlegroups.com
#35071: Resolve lazy objects inside dictionaries when saving JSONFields
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I completely agree with Claude here.

As a matter of fact `DjangoJSONEncoder` already works perfectly for this
use case so I don't understand why we should treat promises differently
[https://docs.djangoproject.com/en/5.0/ref/models/fields/#django.db.models.JSONField.encoder
given we already document using it for such cases].

{{{#!python
import json
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.translation import gettext_lazy as _

json.dumps({"key": _("foobar")})
# TypeError: Object of type __proxy__ is not JSON serializable
json.dumps({"key": _("foobar")}, cls=DjangoJSONEncoder)
>>> '{"key": "foobar"}'
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:12>

Django

unread,
Jan 1, 2024, 3:17:29 PM1/1/24
to django-...@googlegroups.com
#35071: Resolve lazy objects inside dictionaries when saving JSONFields
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
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
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* status: assigned => closed
* has_patch: 1 => 0
* resolution: => invalid
* stage: Accepted => Unreviewed


Comment:

Makes sense. Docs links are very helpful. Sorry for not using a support
channel to check my understanding first!

--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:13>

Reply all
Reply to author
Forward
0 new messages