mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-15 12:08:17 -06:00
Changes needed to implement Custom Field imports via CSV, and enable this when importing devices
This commit is contained in:
parent
6f5c35c278
commit
9cb6b792ba
@ -1419,7 +1419,7 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldForm):
|
|||||||
self.initial['rack'] = self.instance.parent_bay.device.rack_id
|
self.initial['rack'] = self.instance.parent_bay.device.rack_id
|
||||||
|
|
||||||
|
|
||||||
class BaseDeviceCSVForm(forms.ModelForm):
|
class BaseDeviceCSVForm(CustomFieldForm):
|
||||||
device_role = forms.ModelChoiceField(
|
device_role = forms.ModelChoiceField(
|
||||||
queryset=DeviceRole.objects.all(),
|
queryset=DeviceRole.objects.all(),
|
||||||
to_field_name='name',
|
to_field_name='name',
|
||||||
|
@ -12,7 +12,7 @@ from dcim.models import DeviceRole, Platform, Region, Site
|
|||||||
from tenancy.models import Tenant, TenantGroup
|
from tenancy.models import Tenant, TenantGroup
|
||||||
from utilities.forms import (
|
from utilities.forms import (
|
||||||
add_blank_choice, APISelectMultiple, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect, ContentTypeSelect,
|
add_blank_choice, APISelectMultiple, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect, ContentTypeSelect,
|
||||||
FilterChoiceField, FilterTreeNodeMultipleChoiceField, LaxURLField, JSONField, SlugField,
|
CSVChoiceFieldCustom, FilterChoiceField, FilterTreeNodeMultipleChoiceField, LaxURLField, JSONField, SlugField,
|
||||||
)
|
)
|
||||||
from .constants import (
|
from .constants import (
|
||||||
CF_FILTER_DISABLED, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_INTEGER, CF_TYPE_SELECT, CF_TYPE_URL,
|
CF_FILTER_DISABLED, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_INTEGER, CF_TYPE_SELECT, CF_TYPE_URL,
|
||||||
@ -75,7 +75,7 @@ def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=F
|
|||||||
default_choice = cf.choices.get(value=initial).pk
|
default_choice = cf.choices.get(value=initial).pk
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
pass
|
pass
|
||||||
field = forms.TypedChoiceField(choices=choices, coerce=int, required=cf.required, initial=default_choice)
|
field = CSVChoiceFieldCustom(choices=choices, coerce=int, required=cf.required, initial=default_choice)
|
||||||
|
|
||||||
# URL
|
# URL
|
||||||
elif cf.type == CF_TYPE_URL:
|
elif cf.type == CF_TYPE_URL:
|
||||||
|
@ -436,6 +436,26 @@ class CSVChoiceField(forms.ChoiceField):
|
|||||||
return self.choice_values[value]
|
return self.choice_values[value]
|
||||||
|
|
||||||
|
|
||||||
|
class CSVChoiceFieldCustom(forms.TypedChoiceField):
|
||||||
|
"""
|
||||||
|
For custom fields, invert the provided set of choices to take the human-friendly label as input, and return the database value.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, choices=(), coerce=lambda val: val, empty_value='', *args, **kwargs):
|
||||||
|
super().__init__(choices=choices, coerce=coerce, empty_value=empty_value, *args, **kwargs)
|
||||||
|
self.choice_values = {label: value for value, label in unpack_grouped_choices(choices)}
|
||||||
|
|
||||||
|
def clean(self, value):
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
|
||||||
|
for choice in self.choice_values:
|
||||||
|
if str(choice) == value:
|
||||||
|
return self.choice_values[choice]
|
||||||
|
|
||||||
|
return super().clean(value)
|
||||||
|
|
||||||
|
|
||||||
class ExpandableNameField(forms.CharField):
|
class ExpandableNameField(forms.CharField):
|
||||||
"""
|
"""
|
||||||
A field which allows for numeric range expansion
|
A field which allows for numeric range expansion
|
||||||
|
@ -143,6 +143,9 @@ def example_choices(field, arg=3):
|
|||||||
break
|
break
|
||||||
if not value or not label:
|
if not value or not label:
|
||||||
continue
|
continue
|
||||||
|
if hasattr(label, 'value'):
|
||||||
|
# Handling for custom field choice labels
|
||||||
|
label = label.value
|
||||||
examples.append(label)
|
examples.append(label)
|
||||||
return ', '.join(examples) or 'None'
|
return ', '.join(examples) or 'None'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user