Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Last active November 28, 2024 23:53
Show Gist options
  • Save douglasmiranda/17432b5261aa0a5980069bd9bf2de6c5 to your computer and use it in GitHub Desktop.
Save douglasmiranda/17432b5261aa0a5980069bd9bf2de6c5 to your computer and use it in GitHub Desktop.
Django Admin - Pretty Print (formatted / idented / no color) JSON (read-only)
# IMPORTANT: Just keep in mind this is not ideal for untrusted JSON content.
import json
from django.contrib import admin
from .models import Publication
@admin.register(Publication)
class PublicationAdmin(admin.ModelAdmin):
readonly_fields = (
"pretty_meta_info",
)
@admin.display(description="Meta Info")
def pretty_meta_info(self, publication):
pretty_json = json.dumps(publication.meta_info, indent=4, sort_keys=True)
return mark_safe(f"<pre>{pretty_json}</pre>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment