question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The formatting does not work on read-only fields

See original GitHub issue

My code setup is 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.

Is this by design? And is there any way to have the formatting work for read-only fields?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jordanmkonczcommented, Feb 16, 2020

I ran into this issue too. I tried your workaround @BenVosper and while this works for a user who has elevated permissions for a model, if the user only has the “view” permission for a model then Django automatically makes all fields read-only and as such django-prettyjson does nothing and the user just sees the plain text / string version of the JSON.

@kevinmickey any thoughts on how to get django-prettyjson to work for read-only fields?

0reactions
marvinsilvacommented, Jan 14, 2022

Since including a JSONField in readonly_fields prevents the PrettyJSONWidget from being used, instead of overriding ModelAdmin.get_formas suggested, it is possible to pass the disable parameter as an attribute for PrettyJSONWidget too:

formfield_overrides = {
    JSONField: {
        "widget": PrettyJSONWidget(
            attrs={"initial": "parsed", "disabled": True}
        )
    }
}

Working with Django 3.1.13, django-prettyjson 0.4.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Read-only form field formatting - django - Stack Overflow
I've created a custom ModelForm for my ModelAdmin , trying to apply the formatting in the overridden __init__ method. The problem is, I...
Read more >
Disable or set a control as read-only by using conditional ...
On the form template, click the control that you want to disable or set as read-only. · On the Format menu, click Conditional...
Read more >
Solved: Can Text Field formatting be disabled when setting...
I found a workaround, by setting the field to Hidden in the Behaviour it doesn't display the field when the issue is editable,...
Read more >
HTML attribute: readonly - HTML: HyperText Markup Language
The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control.
Read more >
readonly fields not editable by transform map? - ServiceNow
If the field is readonly at dictionary level I can still update it from scripts background so it should be possible via transform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found