The formatting does not work on read-only fields
See original GitHub issueMy 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:
- Created 5 years ago
- Comments:10 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
Since including a JSONField in readonly_fields prevents the PrettyJSONWidget from being used, instead of overriding
ModelAdmin.get_form
as suggested, it is possible to pass thedisable
parameter as an attribute forPrettyJSONWidget
too:Working with Django 3.1.13, django-prettyjson 0.4.1