kevinmickey / django-prettyjson

Enables pretty JSON viewer in Django forms, admin, or templates
BSD 3-Clause "New" or "Revised" License
139 stars 21 forks source link

Not work with readonly_fields #9

Closed seunghokimj closed 6 years ago

seunghokimj commented 7 years ago

Using 0.2.2 on Django 1.9.4 It seems to not work with readonly_fields.

I think PrettyJSONWidget's render may run before render readonly_fields.

kevinmickey commented 6 years ago

Instead of using readonly_fields, use the disabled form field attribute, introduced in Django 1.9. See https://docs.djangoproject.com/en/2.0/ref/forms/fields/#disabled

from prettyjson import PrettyJSONWidget

class JsonForm(forms.ModelForm):
  def __init__(self, *args, **kwargs):
    super(JsonForm, self).__init__(*args, **kwargs)
    self.fields['json'].disabled = True
  class Meta:
    model = Test
    fields = '__all__'
    widgets = {
      'myjsonfield': PrettyJSONWidget(),
    }

class JsonAdmin(admin.ModelAdmin):
  form = JsonForm