Fixes #13843: Fix scope assignment for VLAN groups during bulk edit

This commit is contained in:
Jeremy Stretch 2023-09-25 11:17:12 -04:00
parent bd1cbce2b3
commit 4fe02a7be5
2 changed files with 21 additions and 1 deletions

View File

@ -431,6 +431,10 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES), queryset=ContentType.objects.filter(model__in=VLANGROUP_SCOPE_TYPES),
required=False required=False
) )
scope_id = forms.IntegerField(
required=False,
widget=forms.HiddenInput()
)
region = DynamicModelChoiceField( region = DynamicModelChoiceField(
label=_('Region'), label=_('Region'),
queryset=Region.objects.all(), queryset=Region.objects.all(),
@ -488,6 +492,19 @@ class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
) )
nullable_fields = ('description',) 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): class VLANBulkEditForm(NetBoxModelBulkEditForm):
region = DynamicModelChoiceField( region = DynamicModelChoiceField(

View File

@ -3,6 +3,7 @@ import re
from copy import deepcopy from copy import deepcopy
from django.contrib import messages from django.contrib import messages
from django.contrib.contenttypes.fields import GenericRel
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist, ValidationError from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist, ValidationError
from django.db import transaction, IntegrityError from django.db import transaction, IntegrityError
@ -519,9 +520,11 @@ class BulkEditView(GetReturnURLMixin, BaseMultiObjectView):
model_field = self.queryset.model._meta.get_field(name) model_field = self.queryset.model._meta.get_field(name)
if isinstance(model_field, (ManyToManyField, ManyToManyRel)): if isinstance(model_field, (ManyToManyField, ManyToManyRel)):
m2m_fields[name] = model_field 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: else:
model_fields[name] = model_field model_fields[name] = model_field
except FieldDoesNotExist: except FieldDoesNotExist:
# This form field is used to modify a field rather than set its value directly # This form field is used to modify a field rather than set its value directly
model_fields[name] = None model_fields[name] = None