JsonResponse have a DEFAULT_JSON_ENCODER setting

71 views
Skip to first unread message

Fab

unread,
Jun 23, 2022, 10:54:56 AM6/23/22
to Django developers (Contributions to Django itself)
Hey,

With JsonResponse instead of the encoder defaulting to DjangoJSONEncoder I was thinking it would be nice to have a setting like DEFAULT_JSON_ENCODER so I can set another one if needed without having to change all my usages already set.


Is there any reason not to do this? Feels like it wont break backwards compatibility.

Regards,

Fab

Steve Jorgensen

unread,
Jun 23, 2022, 4:11:02 PM6/23/22
to Django developers (Contributions to Django itself)
I think that seems like a good thing to be able to configure.

Adam Johnson

unread,
Jun 23, 2022, 5:57:14 PM6/23/22
to Django developers (Contributions to Django itself)
I don't think that's necessary, you can create a subclass of JSONField with the encoder you want, or even faster use functools.partial() as I blogged about here: https://adamj.eu/tech/2021/05/05/3-uses-for-functools-partial-in-django/#making-reusable-fields-without-subclassing

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/0eff9638-f455-4a64-a507-b47c6bd2cff0n%40googlegroups.com.

Carlton Gibson

unread,
Jun 23, 2022, 11:44:52 PM6/23/22
to Django developers (Contributions to Django itself)
This is talking about HTTP responses, rather than fields, but I agree with Adam here: you should be able to encapsulate overriding the default encoder in a single location. It doesn't need a setting. 

Reply all
Reply to author
Forward
0 new messages