Fixes #406: Resolve correct to_field_name for CSV/YAML/JSON bulk import#557
Conversation
…odel fields
Bulk import for Object/MultiObject fields referencing models without a
'name' field (e.g. dcim.ModuleType, which uses 'model') failed with
'invalid accessor field name' because to_field_name was hardcoded to
fall back to 'name'.
Add _csv_import_to_field_name() which probes the target model's fields
in order ('name', 'slug', 'model', 'identifier') and uses the first
match, falling back to 'pk'. This mirrors how NetBox core handles
ModuleType in its own bulk import forms (explicit to_field_name='model').
Both ObjectFieldType and MultiObjectFieldType get_form_field() now use
this helper instead of the hardcoded 'name' fallback.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- except Exception -> except FieldDoesNotExist (already imported at top) - if explicit: -> if explicit is not None: to handle empty-string correctly - Add logger.warning() when falling back to 'pk' (user-unfriendly edge case) - Hoist inline test imports to module level (dcim.models, _csv_import_to_field_name) - Remove now-redundant inline imports from test methods - Add test_csv_import_field_uses_slug_before_model_for_device_type to document that DeviceType resolves to 'slug' before 'model', matching NetBox core behaviour - Add MultiObjectFieldType.test_csv_import_form_field_for_module_type_uses_model_field to cover the multi-object import path Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@claude /review and limit response to areas of potential concern. |
|
Claude finished @bctiemann's task in 1m 45s —— View job Review: Areas of Potential Concern
1. Field existence vs. uniqueness (
|
… gaps Concern 1 (uniqueness): the probe loop now checks getattr(field_obj, 'unique', False) and returns on the first unique match. Non-unique candidates are kept as a secondary fallback so behaviour is preserved for edge cases where no globally-unique field exists. Concern 2 (explicit validation): the explicit path now calls _meta.get_field() and falls through to the probe loop with a warning if the stored value is stale (field no longer exists on the model). Concern 3 (test gaps): - test_csv_import_to_field_name_explicit_returned_when_valid: non-None explicit that exists on the model is returned directly. - test_csv_import_to_field_name_explicit_falls_through_when_stale: stale explicit triggers a warning and falls through to the probe loop. - test_csv_import_to_field_name_pk_fallback_when_no_candidate_exists: mock model with no matching fields triggers warning and returns 'pk'. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Fixes: #406
Summary
Bulk import of Object and MultiObject fields referencing models without a
namefield (e.g.dcim.ModuleType, which usesmodelas its identifier) failed with"invalid accessor field name"becauseto_field_namewas hardcoded to fall back to'name'.Root Cause
In
ObjectFieldType.get_form_field()andMultiObjectFieldType.get_form_field(), the CSV import path used:ModuleTypehas nonamefield — it usesmodel— soCSVModelChoiceFieldreceived an invalid accessor and raisedFieldError, surfaced to the user as"invalid accessor field name".NetBox core's own bulk import form for
ModuleTypeexplicitly setsto_field_name='model', confirming'name'is not a universal default.Fix
Add
_csv_import_to_field_name(model_class, explicit=None)which probes the target model's fields in priority order('name', 'slug', 'model', 'identifier')and returns the first match, falling back to'pk'. Bothget_form_field()call sites now use this helper.With this fix, importing a CO instance that references a
ModuleTypeworks as expected:Tests
test_csv_import_field_uses_name_for_models_with_name—Device(hasname) →'name'test_csv_import_field_uses_model_for_module_type—ModuleType(noname) →'model'test_csv_import_form_field_for_module_type_uses_model_field— end-to-end:get_form_field(for_csv_import=True)on aModuleTypeCOTF producesto_field_name='model'🤖 Generated with Claude Code