From 1a9149d7d44de01358207f1b8b74145ecc8e33f7 Mon Sep 17 00:00:00 2001 From: Daniel Sheppard Date: Mon, 5 Feb 2024 08:59:24 -0600 Subject: [PATCH] Fixes: #14839 - Check for tunnel termination type in data and instance in addition to intially passed data. (#14995) * Fixes: #14839 - Check for tunnel termination type in additional instances * Incorporate recommended changes --- netbox/vpn/forms/model_forms.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/netbox/vpn/forms/model_forms.py b/netbox/vpn/forms/model_forms.py index 1a11e62b7..f936de88c 100644 --- a/netbox/vpn/forms/model_forms.py +++ b/netbox/vpn/forms/model_forms.py @@ -265,9 +265,15 @@ class TunnelTerminationForm(NetBoxModelForm): def __init__(self, *args, initial=None, **kwargs): super().__init__(*args, initial=initial, **kwargs) - if initial and initial.get('type') == TunnelTerminationTypeChoices.TYPE_VIRTUALMACHINE: + if (get_field_value(self, 'type') is None and + self.instance.pk and isinstance(self.instance.termination.parent_object, VirtualMachine)): + self.fields['type'].initial = TunnelTerminationTypeChoices.TYPE_VIRTUALMACHINE + + # If initial or self.data is set and the type is a VIRTUALMACHINE type, swap the field querysets. + if get_field_value(self, 'type') == TunnelTerminationTypeChoices.TYPE_VIRTUALMACHINE: self.fields['parent'].label = _('Virtual Machine') self.fields['parent'].queryset = VirtualMachine.objects.all() + self.fields['parent'].widget.attrs['selector'] = 'virtualization.virtualmachine' self.fields['termination'].queryset = VMInterface.objects.all() self.fields['termination'].widget.add_query_params({ 'virtual_machine_id': '$parent',