mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 08:46:10 -06:00
Introduce CSVModelMultipleChoiceField for CSV import tag assignment
This commit is contained in:
parent
8b2ec9444b
commit
450f68c40e
@ -6,9 +6,8 @@ from django.db.models import Q
|
|||||||
from extras.choices import CustomFieldFilterLogicChoices, CustomFieldTypeChoices, CustomFieldVisibilityChoices
|
from extras.choices import CustomFieldFilterLogicChoices, CustomFieldTypeChoices, CustomFieldVisibilityChoices
|
||||||
from extras.forms.mixins import CustomFieldsMixin, SavedFiltersMixin
|
from extras.forms.mixins import CustomFieldsMixin, SavedFiltersMixin
|
||||||
from extras.models import CustomField, Tag
|
from extras.models import CustomField, Tag
|
||||||
from taggit.forms import TagField
|
|
||||||
from utilities.forms import BootstrapMixin, CSVModelForm
|
from utilities.forms import BootstrapMixin, CSVModelForm
|
||||||
from utilities.forms.fields import DynamicModelMultipleChoiceField
|
from utilities.forms.fields import CSVModelMultipleChoiceField, DynamicModelMultipleChoiceField
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'NetBoxModelForm',
|
'NetBoxModelForm',
|
||||||
@ -63,9 +62,11 @@ class NetBoxModelCSVForm(CSVModelForm, NetBoxModelForm):
|
|||||||
"""
|
"""
|
||||||
Base form for creating a NetBox objects from CSV data. Used for bulk importing.
|
Base form for creating a NetBox objects from CSV data. Used for bulk importing.
|
||||||
"""
|
"""
|
||||||
tags = TagField(
|
tags = CSVModelMultipleChoiceField(
|
||||||
|
queryset=Tag.objects.all(),
|
||||||
required=False,
|
required=False,
|
||||||
help_text='Tags (as quoted string: "tag1,tag2")'
|
to_field_name='slug',
|
||||||
|
help_text='Tag slugs separated by commas, encased with double quotes (e.g. "tag1,tag2,tag3")'
|
||||||
)
|
)
|
||||||
|
|
||||||
def _get_custom_fields(self, content_type):
|
def _get_custom_fields(self, content_type):
|
||||||
@ -76,15 +77,6 @@ class NetBoxModelCSVForm(CSVModelForm, NetBoxModelForm):
|
|||||||
def _get_form_field(self, customfield):
|
def _get_form_field(self, customfield):
|
||||||
return customfield.to_form_field(for_csv_import=True)
|
return customfield.to_form_field(for_csv_import=True)
|
||||||
|
|
||||||
def clean_tags(self):
|
|
||||||
data = self.cleaned_data['tags']
|
|
||||||
existing_tags = Tag.objects.values_list('slug', flat=True)
|
|
||||||
for tag in data:
|
|
||||||
if tag.strip().lower() not in existing_tags:
|
|
||||||
raise ValidationError(f"Unknown tag: {tag}")
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
class NetBoxModelBulkEditForm(BootstrapMixin, CustomFieldsMixin, forms.Form):
|
||||||
"""
|
"""
|
||||||
|
@ -16,6 +16,7 @@ __all__ = (
|
|||||||
'CSVDataField',
|
'CSVDataField',
|
||||||
'CSVFileField',
|
'CSVFileField',
|
||||||
'CSVModelChoiceField',
|
'CSVModelChoiceField',
|
||||||
|
'CSVModelMultipleChoiceField',
|
||||||
'CSVMultipleChoiceField',
|
'CSVMultipleChoiceField',
|
||||||
'CSVMultipleContentTypeField',
|
'CSVMultipleContentTypeField',
|
||||||
'CSVTypedChoiceField',
|
'CSVTypedChoiceField',
|
||||||
@ -142,7 +143,7 @@ class CSVModelChoiceField(forms.ModelChoiceField):
|
|||||||
Extends Django's `ModelChoiceField` to provide additional validation for CSV values.
|
Extends Django's `ModelChoiceField` to provide additional validation for CSV values.
|
||||||
"""
|
"""
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid_choice': 'Object not found.',
|
'invalid_choice': 'Object not found: %(value)s',
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
@ -154,6 +155,19 @@ class CSVModelChoiceField(forms.ModelChoiceField):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CSVModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
||||||
|
"""
|
||||||
|
Extends Django's `ModelMultipleChoiceField` to support comma-separated values.
|
||||||
|
"""
|
||||||
|
default_error_messages = {
|
||||||
|
'invalid_choice': 'Object not found: %(value)s',
|
||||||
|
}
|
||||||
|
|
||||||
|
def clean(self, value):
|
||||||
|
value = value.split(',') if value else []
|
||||||
|
return super().clean(value)
|
||||||
|
|
||||||
|
|
||||||
class CSVContentTypeField(CSVModelChoiceField):
|
class CSVContentTypeField(CSVModelChoiceField):
|
||||||
"""
|
"""
|
||||||
CSV field for referencing a single content type, in the form `<app>.<model>`.
|
CSV field for referencing a single content type, in the form `<app>.<model>`.
|
||||||
|
Loading…
Reference in New Issue
Block a user