From 01ba1b8c036a021ba611716e18c8bb792689cd8d Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Wed, 13 Apr 2022 13:43:18 -0400 Subject: [PATCH] Fixes #9118: Fix validation error when importing VM child interfaces --- docs/release-notes/version-3.2.md | 1 + netbox/dcim/forms/bulk_import.py | 10 +++++----- netbox/virtualization/forms/bulk_import.py | 12 ++++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/release-notes/version-3.2.md b/docs/release-notes/version-3.2.md index d4efe536f..c5c20379a 100644 --- a/docs/release-notes/version-3.2.md +++ b/docs/release-notes/version-3.2.md @@ -23,6 +23,7 @@ * [#9096](https://github.com/netbox-community/netbox/issues/9096) - Remove duplicate filter tag when filtering by "none" * [#9100](https://github.com/netbox-community/netbox/issues/9100) - Include position field in module type YAML export * [#9116](https://github.com/netbox-community/netbox/issues/9116) - `assigned_to_interface` filter for IP addresses should not match FHRP group assignments +* [#9118](https://github.com/netbox-community/netbox/issues/9118) - Fix validation error when importing VM child interfaces --- diff --git a/netbox/dcim/forms/bulk_import.py b/netbox/dcim/forms/bulk_import.py index d9c738cc2..b28c16fad 100644 --- a/netbox/dcim/forms/bulk_import.py +++ b/netbox/dcim/forms/bulk_import.py @@ -651,11 +651,11 @@ class InterfaceCSVForm(NetBoxModelCSVForm): super().__init__(data, *args, **kwargs) if data: - # Limit interface choices for parent, bridge and lag to device only - params = {} - if data.get('device'): - params[f"device__{self.fields['device'].to_field_name}"] = data.get('device') - if params: + # Limit choices for parent, bridge, and LAG interfaces to the assigned device + if device := data.get('device'): + params = { + f"device__{self.fields['device'].to_field_name}": device + } self.fields['parent'].queryset = self.fields['parent'].queryset.filter(**params) self.fields['bridge'].queryset = self.fields['bridge'].queryset.filter(**params) self.fields['lag'].queryset = self.fields['lag'].queryset.filter(**params) diff --git a/netbox/virtualization/forms/bulk_import.py b/netbox/virtualization/forms/bulk_import.py index b0315dd95..eab6fc9e7 100644 --- a/netbox/virtualization/forms/bulk_import.py +++ b/netbox/virtualization/forms/bulk_import.py @@ -136,6 +136,18 @@ class VMInterfaceCSVForm(NetBoxModelCSVForm): 'vrf', ) + def __init__(self, data=None, *args, **kwargs): + super().__init__(data, *args, **kwargs) + + if data: + # Limit interface choices for parent & bridge interfaces to the assigned VM + if virtual_machine := data.get('virtual_machine'): + params = { + f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": virtual_machine + } + self.fields['parent'].queryset = self.fields['parent'].queryset.filter(**params) + self.fields['bridge'].queryset = self.fields['bridge'].queryset.filter(**params) + def clean_enabled(self): # Make sure enabled is True when it's not included in the uploaded data if 'enabled' not in self.data: