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

Initial work on #7006

This commit is contained in:
jeremystretch
2021-12-30 17:03:41 -05:00
parent 0978777eec
commit fa1e28e860
10 changed files with 224 additions and 129 deletions

View File

@@ -16,7 +16,8 @@ from extras.utils import FeatureQuery, extras_features
from netbox.models import ChangeLoggedModel
from utilities import filters
from utilities.forms import (
CSVChoiceField, DatePicker, LaxURLField, StaticSelectMultiple, StaticSelect, add_blank_choice,
CSVChoiceField, DatePicker, DynamicModelChoiceField, LaxURLField, StaticSelectMultiple, StaticSelect,
add_blank_choice,
)
from utilities.querysets import RestrictedQuerySet
from utilities.validators import validate_regex
@@ -50,8 +51,17 @@ class CustomField(ChangeLoggedModel):
type = models.CharField(
max_length=50,
choices=CustomFieldTypeChoices,
default=CustomFieldTypeChoices.TYPE_TEXT
default=CustomFieldTypeChoices.TYPE_TEXT,
help_text='The type of data this custom field holds'
)
object_type = models.ForeignKey(
to=ContentType,
on_delete=models.PROTECT,
blank=True,
null=True,
help_text='The type of NetBox object this field maps to (for object fields)'
)
name = models.CharField(
max_length=50,
unique=True,
@@ -122,7 +132,6 @@ class CustomField(ChangeLoggedModel):
null=True,
help_text='Comma-separated list of available choices (for selection fields)'
)
objects = CustomFieldManager()
class Meta:
@@ -234,6 +243,23 @@ class CustomField(ChangeLoggedModel):
'default': f"The specified default value ({self.default}) is not listed as an available choice."
})
def serialize(self, value):
"""
Prepare a value for storage as JSON data.
"""
if self.type == CustomFieldTypeChoices.TYPE_OBJECT and value is not None:
return value.pk
return value
def deserialize(self, value):
"""
Convert JSON data to a Python object suitable for the field type.
"""
if self.type == CustomFieldTypeChoices.TYPE_OBJECT and value is not None:
model = self.object_type.model_class()
return model.objects.filter(pk=value).first()
return value
def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False):
"""
Return a form field suitable for setting a CustomField's value for an object.
@@ -300,6 +326,15 @@ class CustomField(ChangeLoggedModel):
elif self.type == CustomFieldTypeChoices.TYPE_JSON:
field = forms.JSONField(required=required, initial=initial)
# Object
elif self.type == CustomFieldTypeChoices.TYPE_OBJECT:
model = self.object_type.model_class()
field = DynamicModelChoiceField(
queryset=model.objects.all(),
required=required,
initial=initial
)
# Text
else:
if self.type == CustomFieldTypeChoices.TYPE_LONGTEXT: