Skip to content

Commit 95faa66

Browse files
Fixes: #17497 - Handle invalid accessor fields in bulk import forms (#17594)
* Add handling for FieldError to CSVModelChoiceField.to_python to handle invalid accessor field * manufacturer & default_platform should be CSVModelChoiceFields * Fix string translation --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent ab42b00 commit 95faa66

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

netbox/dcim/forms/bulk_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class DeviceTypeImportForm(NetBoxModelImportForm):
374374
to_field_name='name',
375375
help_text=_('The manufacturer which produces this device type')
376376
)
377-
default_platform = forms.ModelChoiceField(
377+
default_platform = CSVModelChoiceField(
378378
label=_('Default platform'),
379379
queryset=Platform.objects.all(),
380380
to_field_name='name',

netbox/utilities/forms/fields/csv.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django import forms
22
from django.utils.translation import gettext_lazy as _
33
from django.contrib.contenttypes.models import ContentType
4-
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
4+
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist, FieldError
55
from django.db.models import Q
66

77
from utilities.choices import unpack_grouped_choices
@@ -64,6 +64,10 @@ def to_python(self, value):
6464
raise forms.ValidationError(
6565
_('"{value}" is not a unique value for this field; multiple objects were found').format(value=value)
6666
)
67+
except FieldError:
68+
raise forms.ValidationError(
69+
_('"{field_name}" is an invalid accessor field name.').format(field_name=self.to_field_name)
70+
)
6771

6872

6973
class CSVModelMultipleChoiceField(forms.ModelMultipleChoiceField):

0 commit comments

Comments
 (0)