From a953ff20f9049ebf7a06a8bcc93400ac6f278059 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Thu, 9 May 2024 11:54:38 -0700 Subject: [PATCH] 15973 fix switch type on cable edit (#16049) * 15973 fix switch type on cable edit * 15973 fix cable add from device --- netbox/dcim/forms/connections.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/netbox/dcim/forms/connections.py b/netbox/dcim/forms/connections.py index b57f4ad2c..44bea047a 100644 --- a/netbox/dcim/forms/connections.py +++ b/netbox/dcim/forms/connections.py @@ -90,14 +90,14 @@ def get_cable_form(a_type, b_type): class _CableForm(CableForm, metaclass=FormMetaclass): def __init__(self, *args, initial=None, **kwargs): - initial = initial or {} + if a_type: - ct = ContentType.objects.get_for_model(a_type) - initial['a_terminations_type'] = f'{ct.app_label}.{ct.model}' + a_ct = ContentType.objects.get_for_model(a_type) + initial['a_terminations_type'] = f'{a_ct.app_label}.{a_ct.model}' if b_type: - ct = ContentType.objects.get_for_model(b_type) - initial['b_terminations_type'] = f'{ct.app_label}.{ct.model}' + b_ct = ContentType.objects.get_for_model(b_type) + initial['b_terminations_type'] = f'{b_ct.app_label}.{b_ct.model}' # TODO: Temporary hack to work around list handling limitations with utils.normalize_querydict() for field_name in ('a_terminations', 'b_terminations'): @@ -108,8 +108,17 @@ def get_cable_form(a_type, b_type): if self.instance and self.instance.pk: # Initialize A/B terminations when modifying an existing Cable instance - self.initial['a_terminations'] = self.instance.a_terminations - self.initial['b_terminations'] = self.instance.b_terminations + if a_type and self.instance.a_terminations and a_ct == ContentType.objects.get_for_model(self.instance.a_terminations[0]): + self.initial['a_terminations'] = self.instance.a_terminations + if b_type and self.instance.b_terminations and b_ct == ContentType.objects.get_for_model(self.instance.b_terminations[0]): + self.initial['b_terminations'] = self.instance.b_terminations + else: + # Need to clear terminations if swapped type - but need to do it only + # if not from instance + if a_type: + initial.pop('a_terminations', None) + if b_type: + initial.pop('b_terminations', None) def clean(self): super().clean()