diff --git a/netbox/vpn/forms/model_forms.py b/netbox/vpn/forms/model_forms.py index 1a11e62b70d..f936de88c3c 100644 --- a/netbox/vpn/forms/model_forms.py +++ b/netbox/vpn/forms/model_forms.py @@ -265,9 +265,15 @@ class Meta: 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',