Customize data appearance in Django Admin interface

I am working with a model that has a json field. The data stored in this field may not always be pretty-printed, and I am okay with it as long as it is valid. However, when the data is displayed in Django admin, I would like it to be pretty printed for easier readability. I don't have an issue if this means the pretty-printed version is saved as well.

Do you have any suggestions on how to achieve this?

Answer №1

Figuring it out on my own...

After delving into the documentation following the lead of Matthew J Morrison, I stumbled upon a way to incorporate javascript for tweaking purposes.

Within my ModelAdmin, I included

class Media:
    js = ("/site_media/json2.js", "/site_media/custom.js")

The json2.js file can be sourced from Douglas Crockford's website

The custom.js file looks like this

django.jQuery(document).ready(function() {
    data = JSON.parse(django.jQuery("#id_json")[0].value);
    django.jQuery("#id_json")[0].value = JSON.stringify(data, null, 4)
});

It's a breeze when you have the right know-how.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating unique spreadsheet identifiers in an HTML file

When it comes to displaying a chart using Google Sheets, I encounter an issue where the data is sourced from the same spreadsheet: function updateChart() { var ui = HtmlService.createHtmlOutputFromFile('ChartLine').setWidth(1350).setHeight(550) ...

Convert JSON strings with varying formats into objects of a uniform Java class

How can I deserialize JSON strings with different formats into instances of the same Java class using Jackson efficiently? Let's say I have information about users from various sources: Format 1: "user" : { "name" : "John", "age" : 21, ...

Creating a multi-file upload feature in Django

Looking for a way to achieve multiple file uploads in Django with two models? I have one form but need to handle data for two separate models models.py class Advertisement(models.Model): title = models.CharField(max_length=85, blank=False) class Ima ...

Setting up a Node.js http2 server along with an http server specifically designed for a single

In my upcoming project, I am interested in implementing the http2 protocol. My goal is to have both http and http2 servers running on a single domain and port, if feasible. When a client that does not support http2 connects, communication will default to ...

Tips on incorporating the Browserified npm package into an Angular controller

After spending countless hours searching for a solution to integrate Browserify with my project, I found myself struggling to find a tutorial or example that addressed my specific issue. Most resources focused on bundling code rather than demonstrating how ...

Bring in all SCSS styles from a single file and apply them to a React component

I am attempting to incorporate the entire SCSS file into a React component. I attempted to use the styleName props but was unsuccessful import React from 'react' import Calendar from 'calendar' import { calendarStyles } from './ ...

Enable strict mode for older web browsers

I would like to incorporate the "use strict"; statement into my function, but unfortunately it is not compatible with older browsers such as ie7 and ie8. Is there a workaround to ensure this functionality works in legacy browsers? Could someone please cla ...

Trigger the click event on a specific class selector to extract the corresponding ID

I have an HTML Table with each row: <table> <tr><td><a href='#' id='1' class='delete'>delete</a></td></tr> <tr><td><a href='#' id='2' class='de ...

Exploring the differences between __exact and get()

Why use the __exact query lookup when we can easily retrieve data using get(). What additional advantages does __exact provide in querysets? ...

What is the best way to populate a table view with an array of data when the Next button is tapped in iOS using Swift

Currently, I am attempting to load detailed array items into a table view. The table view consists of an array of labels and two buttons placed on the navigation bar labeled Next and Previous. When the user taps on the Next button, it will replace the arr ...

Dealing with ng-repeat in Angular can be challenging when working with JSON data

Apologies if this question has been asked before, I tried searching but couldn't find an answer. Issue: When calling a web service, I am getting poorly formed JSON data from a Dynamics Nav service: JSON: "[{\"type\":\"2\",&bsol ...

Deciphering varied data

Is there a method for serializing JSON data in such a way that it can be deserialized in parts or sections? Imagine if the top portion of the data contained a "code" indicating how to handle the bottom portion ... for example, deserializing the bottom hal ...

What is causing my Directive to trigger the error "Error: $injector:unpr Unknown Provider"?

I have been diligently working on updating my Controllers, Factories, and Directives to align with the recommended Angular Style Guide for Angular Snippets. So far, I have successfully refactored the Controllers and Factories to comply with the new style ...

Uploading a collection of objects to Node.js for storage in a database with Mongoose

I have developed a ReactJS form that allows users to dynamically add form "parts" (sections with form input fields). Let me illustrate this concept with an example: <div> <input placeholder="Author" /> <input placeholder="Age" /> ...

The image slider is blocking the dropdown functionality of the navbar on mobile devices

My code is experiencing a conflict of events. I have created a menu bar using nav bar, as well as an image slider called the caroussel. The issue arises when the window is minimized - the menu bar fails to drop down properly with the presence of the caro ...

What is the best way to conceal an element by clicking anywhere other than on the element itself?

Currently, I am utilizing jQuery's toggle method to display or hide a page element when another specific page element is clicked. jQuery('a#mobile-search-icon).click(function(){ jQuery('#searchBox').toggle('slow'); }); ...

Having trouble with my initialData in react-query - any suggestions on how to fix it?

I encountered an issue while trying to implement code using initialData in React Query. The output does not display as expected, and the key is not visible in the react-query-dev-tool. View image here import { useQuery, useQueryClient } from 'react-qu ...

How can I alter the div once the form has been submitted?

Is there a way to change the background of a form and its results after submitting the form? I need to switch the image background to a plain color. I attempted to use a solution I found, but it doesn't seem to be working as expected. You can view my ...

Effortless form submission through Angular

When attempting to create a form submit using Angular $http, the following codes were utilized. HTML (within the controller div): <input type="text" name="job" ng-model="item.job"> <button type="submit" ng-click="post()">Add</button> J ...

Uh oh! The site you are trying to access at /admin/login does not exist. Please try again or contact the admin for assistance

After completing Corey's blogging YouTube series and Mitchell's ecommerce Udemy course, I attempted to combine the two by integrating mapping functionality. This required me to switch the production database I was using, which may have caused som ...