How do I format a read-only field in the Django admin with a 3rd party library?

Viewed 380

I am using django-prettyjson to format my JSONFields in the admin. My code is setup as follows:

models.py

class MyModel(models.Model):
    structure = JSONField(default=dict, blank=True, null=True)
    static_structure = JSONField(default=dict, blank=True, null=True, editable=False)

admin.py

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        JSONField: {'widget': PrettyJSONWidget }
    }

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ('static_structure')
        else:
            return super(MyModelAdmin, self).get_readonly_fields(request, obj)

admin.site.register(MyModel, MyModelAdmin)

In other words, one of the JSONFields is not editable and read-only. It appears on my admin panel, but its not being affected by the widget which renders the other field as formatted JSON.

Any way to fix this? Looking through plain-text JSON data is a complete drag. Thanks!

0 Answers
Related