14
14
from django .utils .translation import gettext_lazy as _
15
15
from extras .choices import CustomFieldTypeChoices , CustomFieldUIEditableChoices
16
16
from utilities .api import get_serializer_for_model
17
- from utilities .forms .fields import (CSVChoiceField , CSVMultipleChoiceField ,
17
+ from utilities .forms .fields import (CSVChoiceField , CSVModelChoiceField ,
18
+ CSVModelMultipleChoiceField , CSVMultipleChoiceField ,
18
19
DynamicChoiceField ,
19
20
DynamicMultipleChoiceField , JSONField ,
20
21
LaxURLField )
@@ -357,7 +358,6 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
357
358
"""
358
359
content_type = ContentType .objects .get (pk = field .related_object_type_id )
359
360
360
- from utilities .forms .fields import DynamicModelChoiceField
361
361
if content_type .app_label == APP_LABEL :
362
362
# This is a custom object type
363
363
from netbox_custom_objects .models import CustomObjectType
@@ -367,24 +367,34 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
367
367
)
368
368
custom_object_type = CustomObjectType .objects .get (pk = custom_object_type_id )
369
369
model = custom_object_type .get_model ()
370
- field_class = DynamicModelChoiceField
371
370
else :
372
371
# This is a regular NetBox model
373
372
model = content_type .model_class ()
374
373
374
+ if for_csv_import :
375
+ field_class = CSVModelChoiceField
376
+ # For CSV import, determine to_field_name from the field configuration
377
+ to_field_name = getattr (field , 'to_field_name' , None ) or 'name'
378
+ return field_class (
379
+ queryset = model .objects .all (),
380
+ required = field .required ,
381
+ initial = field .default ,
382
+ to_field_name = to_field_name ,
383
+ )
384
+ else :
385
+ from utilities .forms .fields import DynamicModelChoiceField
375
386
field_class = DynamicModelChoiceField
376
-
377
- return field_class (
378
- queryset = model .objects .all (),
379
- required = field .required ,
380
- initial = field .default ,
381
- query_params = (
382
- field .related_object_filter
383
- if hasattr (field , "related_object_filter" )
384
- else None
385
- ),
386
- selector = True ,
387
- )
387
+ return field_class (
388
+ queryset = model .objects .all (),
389
+ required = field .required ,
390
+ initial = field .default ,
391
+ query_params = (
392
+ field .related_object_filter
393
+ if hasattr (field , "related_object_filter" )
394
+ else None
395
+ ),
396
+ selector = True ,
397
+ )
388
398
389
399
def get_filterform_field (self , field , ** kwargs ):
390
400
return None
@@ -658,19 +668,30 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
658
668
# This is a regular NetBox model
659
669
model = content_type .model_class ()
660
670
661
- from utilities .forms .fields import DynamicModelMultipleChoiceField
662
-
663
- return DynamicModelMultipleChoiceField (
664
- queryset = model .objects .all (),
665
- required = field .required ,
666
- initial = field .default ,
667
- query_params = (
668
- field .related_object_filter
669
- if hasattr (field , "related_object_filter" )
670
- else None
671
- ),
672
- selector = True ,
673
- )
671
+ if for_csv_import :
672
+ field_class = CSVModelMultipleChoiceField
673
+ # For CSV import, determine to_field_name from the field configuration
674
+ to_field_name = getattr (field , 'to_field_name' , None ) or 'name'
675
+ return field_class (
676
+ queryset = model .objects .all (),
677
+ required = field .required ,
678
+ initial = field .default ,
679
+ to_field_name = to_field_name ,
680
+ )
681
+ else :
682
+ from utilities .forms .fields import DynamicModelMultipleChoiceField
683
+ field_class = DynamicModelMultipleChoiceField
684
+ return field_class (
685
+ queryset = model .objects .all (),
686
+ required = field .required ,
687
+ initial = field .default ,
688
+ query_params = (
689
+ field .related_object_filter
690
+ if hasattr (field , "related_object_filter" )
691
+ else None
692
+ ),
693
+ selector = True ,
694
+ )
674
695
675
696
def get_filterform_field (self , field , ** kwargs ):
676
697
return None
0 commit comments