HTML and JSON fields formatting in Admin's ModelForm

Hey everyone!

I’m planning to apply indentation and syntax highlighting for the two mentioned fields’ contents of a model in my App’s Admin UI. Both of the fields should remain read-only.

I was thinking of extending the default admin’s template(s), or trying to handle my case using custom ModelForm with widgets or third party packages.

What’s the best approach to have the desired results?


I don’t know about “best”, but if I had to approach something like this, I’d first try using the readonly_fields with a custom ModelAdmin method to format the contents of those fields. (This also means that I would not include the original fields in that ModelAdmin class.)

See the third paragraph of the docs and example at ModelAdmin.readonly_fields.

This is only practical if you have a reasonable number of models and fields where you need to do this. If you’ve got a large number of models and fields, I’d be looking at a “higher level” solution by creating a custom ModelAdmin subclass to handle this parametrically, and then have my model admin classes inherit from it.

Thanks @KenWhitesell!

I’m focusing only on the two fields of a single Model. Replacing the original fields with their customised “siblings” sounds great. The indentation part could be easily handled for both HTML and JSON fields. So the simplest solution for syntax highlighting of the given fields seems to be usage of some third party app, covering my custom fields.