Hello,
I’m using the JSONField provided by django.contrib.postgres for logging
arbitrary data to PostgreSQL.
For this use case, I’d like my data to be JSON serialized with as little fuss
as possible. Unfortunately lots of things (dates, datetimes, decimals, etc.)
aren’t natively JSON-serializable and django.contrib.postgres doesn’t seem to
provide a way to override the serializer.
I can wire Django’s JSON encoder, which provides reasonable encoding rules for
all data types I need, by monkey-patching JSONField:
# Monkey-patch django.contrib.postgres to use a smarter JSON serializer.
import json
from django.contrib.postgres.fields import jsonb
from django.core.serializers.json import DjangoJSONEncoder
class DjangoJson(jsonb.Json):
def dumps(self, obj):
return json.dumps(obj, cls=DjangoJSONEncoder)
jsonb.Json = DjangoJson
Is this a common problem? Should JSONField accept additional kwargs to
customize the encoder and the decoder?
Best regards,
--
Aymeric.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/78D28A81-5338-4FC8-8D46-46595DF737F5%40polytechnique.org.
For more options, visit https://groups.google.com/d/optout.