diff --git a/CHANGELOG.md b/CHANGELOG.md index de80e2ad9..4e146f72c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ v2.4.5 (FUTURE) +## Enhancements + +* [#2402](https://github.com/digitalocean/netbox/issues/2402) - Order and format JSON data in form fields + ## Bug Fixes * [#2406](https://github.com/digitalocean/netbox/issues/2406) - Remove hard-coded limit of 1000 objects from API-populated form fields diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 1e6e3c0c4..47ba3744f 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -2,11 +2,12 @@ from __future__ import unicode_literals import csv from io import StringIO +import json import re from django import forms from django.conf import settings -from django.contrib.postgres.forms import JSONField as _JSONField +from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput from django.db.models import Count from django.urls import reverse_lazy from mptt.forms import TreeNodeMultipleChoiceField @@ -556,9 +557,11 @@ class JSONField(_JSONField): self.widget.attrs['placeholder'] = '' def prepare_value(self, value): + if isinstance(value, InvalidJSONInput): + return value if value is None: return '' - return super(JSONField, self).prepare_value(value) + return json.dumps(value, sort_keys=True, indent=4) #