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.
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>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/17668 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/35071#comment:10>
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>
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>
* 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>