Skip to content

Commit

Permalink
Fixes #15896: Retain proper formatting for JSON custom field default …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
jeremystretch committed Apr 30, 2024
1 parent 11816b4 commit 365bb4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion netbox/extras/models/customfields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import decimal
import json
import re
from datetime import datetime, date

Expand Down Expand Up @@ -484,7 +485,7 @@ def to_form_field(self, set_initial=True, enforce_required=True, enforce_visibil

# JSON
elif self.type == CustomFieldTypeChoices.TYPE_JSON:
field = JSONField(required=required, initial=initial)
field = JSONField(required=required, initial=json.dumps(initial) if initial else '')

# Object
elif self.type == CustomFieldTypeChoices.TYPE_OBJECT:
Expand Down
8 changes: 7 additions & 1 deletion netbox/netbox/forms/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django import forms
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
Expand Down Expand Up @@ -34,7 +36,11 @@ def _get_content_type(self):
def _get_form_field(self, customfield):
if self.instance.pk:
form_field = customfield.to_form_field(set_initial=False)
form_field.initial = self.instance.custom_field_data.get(customfield.name, None)
initial = self.instance.custom_field_data.get(customfield.name)
if customfield.type == CustomFieldTypeChoices.TYPE_JSON:
form_field.initial = json.dumps(initial)
else:
form_field.initial = initial
return form_field

return customfield.to_form_field()
Expand Down

0 comments on commit 365bb4b

Please sign in to comment.