Skip to content

Instantly share code, notes, and snippets.

@elky
Created December 23, 2016 13:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elky/0cfe89a89b892e47e8418e281dfe318d to your computer and use it in GitHub Desktop.
Save elky/0cfe89a89b892e47e8418e281dfe318d to your computer and use it in GitHub Desktop.
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