Skip to content

Instantly share code, notes, and snippets.

@elky elky/example.py
Created Dec 23, 2016

Embed
What would you like to do?
Django JSONField prevent Unicode sequence in Admin
# 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
You can’t perform that action at this time.