Created
December 23, 2016 13:50
-
-
Save elky/0cfe89a89b892e47e8418e281dfe318d to your computer and use it in GitHub Desktop.
Django JSONField prevent Unicode sequence in Admin
This file contains 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
# utils.py | |
import json | |
from django.contrib.postgres.forms.jsonb import InvalidJSONInput, JSONField | |
class ReadableJSONFormField(JSONField): | |
def prepare_value(self, value): | |
if isinstance(value, InvalidJSONInput): | |
return value | |
return json.dumps(value, ensure_ascii=False, indent=4) | |
# admin.py | |
from django.contrib import admin | |
from django.contrib.postgres.fields import JSONField | |
from .utils import ReadableJSONFormField | |
@admin.register(Example) | |
class ExampleAdmin(admin.ModelAdmin): | |
formfield_overrides = { | |
JSONField: {'form_class': ReadableJSONFormField}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment