Fixes: #18582 Bulk import prefixes with associated VLAN not working when multiple VLANs with the same vid exist. (#18844)

* Add site CSVModelChoiceField

* Change site field to vlan_site
This commit is contained in:
Renato Almeida de Oliveira 2025-03-11 09:46:46 -03:00 committed by GitHub
parent 89e3f3d3e9
commit 5d81f911d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -177,6 +177,13 @@ class PrefixImportForm(ScopedImportForm, NetBoxModelImportForm):
to_field_name='name', to_field_name='name',
help_text=_("VLAN's group (if any)") help_text=_("VLAN's group (if any)")
) )
vlan_site = CSVModelChoiceField(
label=_('VLAN Site'),
queryset=Site.objects.all(),
required=False,
to_field_name='name',
help_text=_("VLAN's site (if any)")
)
vlan = CSVModelChoiceField( vlan = CSVModelChoiceField(
label=_('VLAN'), label=_('VLAN'),
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
@ -200,8 +207,8 @@ class PrefixImportForm(ScopedImportForm, NetBoxModelImportForm):
class Meta: class Meta:
model = Prefix model = Prefix
fields = ( fields = (
'prefix', 'vrf', 'tenant', 'vlan_group', 'vlan', 'status', 'role', 'scope_type', 'scope_id', 'is_pool', 'prefix', 'vrf', 'tenant', 'vlan_group', 'vlan_site', 'vlan', 'status', 'role', 'scope_type', 'scope_id',
'mark_utilized', 'description', 'comments', 'tags', 'is_pool', 'mark_utilized', 'description', 'comments', 'tags',
) )
labels = { labels = {
'scope_id': _('Scope ID'), 'scope_id': _('Scope ID'),
@ -213,19 +220,19 @@ class PrefixImportForm(ScopedImportForm, NetBoxModelImportForm):
if not data: if not data:
return return
site = data.get('site') vlan_site = data.get('vlan_site')
vlan_group = data.get('vlan_group') vlan_group = data.get('vlan_group')
# Limit VLAN queryset by assigned site and/or group (if specified) # Limit VLAN queryset by assigned site and/or group (if specified)
query = Q() query = Q()
if site: if vlan_site:
query |= Q(**{ query |= Q(**{
f"site__{self.fields['site'].to_field_name}": site f"site__{self.fields['vlan_site'].to_field_name}": vlan_site
}) })
# Don't Forget to include VLANs without a site in the filter # Don't Forget to include VLANs without a site in the filter
query |= Q(**{ query |= Q(**{
f"site__{self.fields['site'].to_field_name}__isnull": True f"site__{self.fields['vlan_site'].to_field_name}__isnull": True
}) })
if vlan_group: if vlan_group: