diff --git a/netbox/extras/forms.py b/netbox/extras/forms.py index 6e8e90512..16c38e61d 100644 --- a/netbox/extras/forms.py +++ b/netbox/extras/forms.py @@ -11,7 +11,7 @@ from taggit.forms import TagField from taggit.models import Tag from dcim.models import Region -from utilities.forms import add_blank_choice, BootstrapMixin, BulkEditForm, LaxURLField, SlugField +from utilities.forms import add_blank_choice, BootstrapMixin, BulkEditForm, LaxURLField, JSONField, SlugField from .constants import ( CF_FILTER_DISABLED, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_INTEGER, CF_TYPE_SELECT, CF_TYPE_URL, OBJECTCHANGE_ACTION_CHOICES, @@ -213,6 +213,7 @@ class ConfigContextForm(BootstrapMixin, forms.ModelForm): queryset=Region.objects.all(), required=False ) + data = JSONField() class Meta: model = ConfigContext diff --git a/netbox/templates/extras/object_configcontext.html b/netbox/templates/extras/object_configcontext.html index 5cc7e7d7f..81f8e1780 100644 --- a/netbox/templates/extras/object_configcontext.html +++ b/netbox/templates/extras/object_configcontext.html @@ -31,6 +31,10 @@ {% endif %}
{{ context.data|render_json }}
+ {% empty %} +
+ None found +
{% endfor %} diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 305c12442..9a4284054 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -6,6 +6,7 @@ import re from django import forms from django.conf import settings +from django.contrib.postgres.forms import JSONField as _JSONField from django.db.models import Count from django.urls import reverse_lazy from mptt.forms import TreeNodeMultipleChoiceField @@ -536,6 +537,22 @@ class LaxURLField(forms.URLField): default_validators = [EnhancedURLValidator()] +class JSONField(_JSONField): + """ + Custom wrapper around Django's built-in JSONField to avoid presenting "null" as the default text. + """ + def __init__(self, *args, **kwargs): + super(JSONField, self).__init__(*args, **kwargs) + if not self.help_text: + self.help_text = 'Enter context data in JSON format.' + self.widget.attrs['placeholder'] = '' + + def prepare_value(self, value): + if value is None: + return '' + return super(JSONField, self).prepare_value(value) + + # # Forms #