Skip to content

Commit

Permalink
Fixes: #17497 - Handle invalid accessor fields in bulk import forms (#…
Browse files Browse the repository at this point in the history
…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 <jstretch@netboxlabs.com>
  • Loading branch information
bctiemann and jeremystretch authored Sep 26, 2024
1 parent 0ec632e commit 52a0b45
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion netbox/dcim/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class DeviceTypeImportForm(NetBoxModelImportForm):
to_field_name='name',
help_text=_('The manufacturer which produces this device type')
)
default_platform = forms.ModelChoiceField(
default_platform = CSVModelChoiceField(
label=_('Default platform'),
queryset=Platform.objects.all(),
to_field_name='name',
Expand Down
6 changes: 5 additions & 1 deletion netbox/utilities/forms/fields/csv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django import forms
from django.utils.translation import gettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist, FieldError
from django.db.models import Q

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


class CSVModelMultipleChoiceField(forms.ModelMultipleChoiceField):
Expand Down

0 comments on commit 52a0b45

Please sign in to comment.