1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #5400: Store custom field defaults as JSON values

This commit is contained in:
Jeremy Stretch
2020-12-01 16:05:23 -05:00
parent 0b57389af6
commit cc271aefe1
8 changed files with 58 additions and 17 deletions

View File

@ -115,10 +115,11 @@ class CustomField(models.Model):
help_text='Loose matches any instance of a given string; exact '
'matches the entire field.'
)
default = models.CharField(
max_length=100,
default = models.JSONField(
blank=True,
help_text='Default value for the field. Use "true" or "false" for booleans.'
null=True,
help_text='Default value for the field (must be a JSON value). Encapsulate '
'strings with double quotes (e.g. "Foo").'
)
weight = models.PositiveSmallIntegerField(
default=100,
@ -171,6 +172,15 @@ class CustomField(models.Model):
obj.save()
def clean(self):
# Validate the field's default value (if any)
if self.default is not None:
try:
self.validate(self.default)
except ValidationError as err:
raise ValidationError({
'default': f'Invalid default value "{self.default}": {err.message}'
})
# Minimum/maximum values can be set only for numeric fields
if self.validation_minimum is not None and self.type != CustomFieldTypeChoices.TYPE_INTEGER:
raise ValidationError({
@ -232,8 +242,6 @@ class CustomField(models.Model):
(True, 'True'),
(False, 'False'),
)
if initial is not None:
initial = bool(initial)
field = forms.NullBooleanField(
required=required, initial=initial, widget=StaticSelect2(choices=choices)
)