In my Django admin im currently displaying JSONField as readonly, the JSON data is quite large and takes up about 80-90% of the page. I was able to find an answer for how one might resize a not readonly field here but other then that I am lost. My ideal outcome would have the large JSON data in a scrollable text box like so: Ideal outcome
How can I control the size of a readonly field without truncating the data?
H0lyBruh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
The easiest way is to create a “custom-field” method in ModelAdmin instead of a readonly field in the form and display it as a scrollable div:
class MyModelAdmin(ModelAdmin):
fields = ...., my_custom_field_mehod, ...
readonly_fields = ...., my_custom_field_mehod, ...
def my_custom_field_mehod(self, object=None):
if obj:
return mark_safe(f'<div class="scrollable">{obj.my_json_data}</div>')
return ''
More info about “custom-field” here:
https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields