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

Merge pull request #4564 from netbox-community/3147-csv-import-fields

Closes #3147: Allow dynamic access to related objects during CSV import
This commit is contained in:
Jeremy Stretch
2020-05-06 10:15:00 -04:00
committed by GitHub
17 changed files with 706 additions and 879 deletions

View File

@@ -14,9 +14,9 @@ from tenancy.forms import TenancyFilterForm, TenancyForm
from tenancy.models import Tenant
from utilities.forms import (
add_blank_choice, APISelect, APISelectMultiple, BootstrapMixin, BulkEditForm, BulkEditNullBooleanSelect,
CommentField, ConfirmationForm, CSVChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
ExpandableNameField, form_from_model, JSONField, SlugField, SmallTextarea, StaticSelect2, StaticSelect2Multiple,
TagFilterField,
CommentField, ConfirmationForm, CSVChoiceField, CSVModelChoiceField, CSVModelForm, DynamicModelChoiceField,
DynamicModelMultipleChoiceField, ExpandableNameField, form_from_model, JSONField, SlugField, SmallTextarea,
StaticSelect2, StaticSelect2Multiple, TagFilterField,
)
from .choices import *
from .models import Cluster, ClusterGroup, ClusterType, VirtualMachine
@@ -36,15 +36,12 @@ class ClusterTypeForm(BootstrapMixin, forms.ModelForm):
]
class ClusterTypeCSVForm(forms.ModelForm):
class ClusterTypeCSVForm(CSVModelForm):
slug = SlugField()
class Meta:
model = ClusterType
fields = ClusterType.csv_headers
help_texts = {
'name': 'Name of cluster type',
}
#
@@ -61,15 +58,12 @@ class ClusterGroupForm(BootstrapMixin, forms.ModelForm):
]
class ClusterGroupCSVForm(forms.ModelForm):
class ClusterGroupCSVForm(CSVModelForm):
slug = SlugField()
class Meta:
model = ClusterGroup
fields = ClusterGroup.csv_headers
help_texts = {
'name': 'Name of cluster group',
}
#
@@ -101,40 +95,28 @@ class ClusterForm(BootstrapMixin, TenancyForm, CustomFieldModelForm):
class ClusterCSVForm(CustomFieldModelCSVForm):
type = forms.ModelChoiceField(
type = CSVModelChoiceField(
queryset=ClusterType.objects.all(),
to_field_name='name',
help_text='Name of cluster type',
error_messages={
'invalid_choice': 'Invalid cluster type name.',
}
help_text='Type of cluster'
)
group = forms.ModelChoiceField(
group = CSVModelChoiceField(
queryset=ClusterGroup.objects.all(),
to_field_name='name',
required=False,
help_text='Name of cluster group',
error_messages={
'invalid_choice': 'Invalid cluster group name.',
}
help_text='Assigned cluster group'
)
site = forms.ModelChoiceField(
site = CSVModelChoiceField(
queryset=Site.objects.all(),
to_field_name='name',
required=False,
help_text='Name of assigned site',
error_messages={
'invalid_choice': 'Invalid site name.',
}
help_text='Assigned site'
)
tenant = forms.ModelChoiceField(
tenant = CSVModelChoiceField(
queryset=Tenant.objects.all(),
to_field_name='name',
required=False,
help_text='Name of assigned tenant',
error_messages={
'invalid_choice': 'Invalid tenant name'
}
help_text='Assigned tenant'
)
class Meta:
@@ -407,42 +389,30 @@ class VirtualMachineCSVForm(CustomFieldModelCSVForm):
required=False,
help_text='Operational status of device'
)
cluster = forms.ModelChoiceField(
cluster = CSVModelChoiceField(
queryset=Cluster.objects.all(),
to_field_name='name',
help_text='Name of parent cluster',
error_messages={
'invalid_choice': 'Invalid cluster name.',
}
help_text='Assigned cluster'
)
role = forms.ModelChoiceField(
role = CSVModelChoiceField(
queryset=DeviceRole.objects.filter(
vm_role=True
),
required=False,
to_field_name='name',
help_text='Name of functional role',
error_messages={
'invalid_choice': 'Invalid role name.'
}
help_text='Functional role'
)
tenant = forms.ModelChoiceField(
tenant = CSVModelChoiceField(
queryset=Tenant.objects.all(),
required=False,
to_field_name='name',
help_text='Name of assigned tenant',
error_messages={
'invalid_choice': 'Tenant not found.'
}
help_text='Assigned tenant'
)
platform = forms.ModelChoiceField(
platform = CSVModelChoiceField(
queryset=Platform.objects.all(),
required=False,
to_field_name='name',
help_text='Name of assigned platform',
error_messages={
'invalid_choice': 'Invalid platform.',
}
help_text='Assigned platform'
)
class Meta: