Last active
November 27, 2017 16:41
-
-
Save rod-dot-codes/8294d4a2565c7c2cac3409d10a183302 to your computer and use it in GitHub Desktop.
A simple monkey patch that makes using Django and JSON a bit easier. Install `django_json_widget` first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.postgres.fields.jsonb import JSONField | |
from django.contrib.postgres.forms.jsonb import JSONField as JSONFormField | |
from django_json_widget.widgets import JSONEditorWidget | |
from django.core.serializers.json import DjangoJSONEncoder | |
JSONFormField.widget = JSONEditorWidget | |
def init_jsonfield(self, verbose_name=None, name=None, encoder=DjangoJSONEncoder, **kwargs): | |
if encoder and not callable(encoder): | |
raise ValueError("The encoder parameter must be a callable object.") | |
self.encoder = encoder | |
super(JSONField, self).__init__(verbose_name, name, **kwargs) | |
JSONField.__init__ = init_jsonfield |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment