mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fixes #15896: Retain proper formatting for JSON custom field default values
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
import decimal
|
import decimal
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
from datetime import datetime, date
|
from datetime import datetime, date
|
||||||
|
|
||||||
@ -484,7 +485,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
|||||||
|
|
||||||
# JSON
|
# JSON
|
||||||
elif self.type == CustomFieldTypeChoices.TYPE_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
|
# Object
|
||||||
elif self.type == CustomFieldTypeChoices.TYPE_OBJECT:
|
elif self.type == CustomFieldTypeChoices.TYPE_OBJECT:
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
@ -34,7 +36,11 @@ class NetBoxModelForm(BootstrapMixin, CheckLastUpdatedMixin, CustomFieldsMixin,
|
|||||||
def _get_form_field(self, customfield):
|
def _get_form_field(self, customfield):
|
||||||
if self.instance.pk:
|
if self.instance.pk:
|
||||||
form_field = customfield.to_form_field(set_initial=False)
|
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 form_field
|
||||||
|
|
||||||
return customfield.to_form_field()
|
return customfield.to_form_field()
|
||||||
|
Reference in New Issue
Block a user