Skip to content

Commit

Permalink
Fixes #13843: Fix scope assignment for VLAN groups during bulk edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Sep 25, 2023
1 parent 577e79f commit a6a8630
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions netbox/ipam/forms/bulk_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
required=False
)
scope_id = forms.IntegerField(
required=False,
widget=forms.HiddenInput()
)
region = DynamicModelChoiceField(
label=_('Region'),
queryset=Region.objects.all(),
Expand Down Expand Up @@ -488,6 +492,19 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
)
nullable_fields = ('description',)

def clean(self):
super().clean()

# Assign scope based on scope_type
if self.cleaned_data.get('scope_type'):
scope_field = self.cleaned_data['scope_type'].model
if scope_obj := self.cleaned_data.get(scope_field):
self.cleaned_data['scope_id'] = scope_obj.pk
self.changed_data.append('scope_id')
else:
self.cleaned_data.pop('scope_type')
self.changed_data.remove('scope_type')


class VLANBulkEditForm(NetBoxModelBulkEditForm):
region = DynamicModelChoiceField(
Expand Down
5 changes: 4 additions & 1 deletion netbox/netbox/views/generic/bulk_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from copy import deepcopy

from django.contrib import messages
from django.contrib.contenttypes.fields import GenericRel
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist, ValidationError
from django.db import transaction, IntegrityError
Expand Down Expand Up @@ -519,9 +520,11 @@ def _update_objects(self, form, request):
model_field = self.queryset.model._meta.get_field(name)
if isinstance(model_field, (ManyToManyField, ManyToManyRel)):
m2m_fields[name] = model_field
elif isinstance(model_field, GenericRel):
# Ignore generic relations (these may be used for other purposes in the form)
continue
else:
model_fields[name] = model_field

except FieldDoesNotExist:
# This form field is used to modify a field rather than set its value directly
model_fields[name] = None
Expand Down

0 comments on commit a6a8630

Please sign in to comment.