Opened 5 years ago

Closed 5 years ago

#31157 closed Bug (fixed)

JSONField are not properly displayed in admin when they are readonly.

Reported by: Gagaro Owned by: dbxnr
Component: contrib.admin Version: 3.0
Severity: Normal Keywords: admin json
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

JSONField values are displayed as dict when readonly in the admin.

For example, {"foo": "bar"} would be displayed as {'foo': 'bar'}, which is not valid JSON.

I believe the fix would be to add a special case in django.contrib.admin.utils.display_for_field to call the prepare_value of the JSONField (not calling json.dumps directly to take care of the InvalidJSONInput case).

Change History (6)

comment:1 by Claude Paroz, 5 years ago

Triage Stage: UnreviewedAccepted

comment:2 by dbxnr, 5 years ago

Owner: changed from nobody to dbxnr
Status: newassigned

comment:3 by dbxnr, 5 years ago

Has patch: set

comment:4 by Simon Charette, 5 years ago

The proposed patch is problematic as the first version coupled contrib.postgres with .admin and the current one is based off the type name which is brittle and doesn't account for inheritance.

It might be worth waiting for #12990 to land before proceeding here as the patch will be able to simply rely of django.db.models.JSONField instance checks from that point.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 675ce4d:

Refs #31157 -- Added tests for display_for_field() with empty JSONField.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In fc0f7f6c:

Fixed #31157 -- Fixed displaying read-only JSONField values in admin.

Note: See TracTickets for help on using tickets.
Back to Top