فهرست منبع

15973 fix switch type on cable edit (#16049)

* 15973 fix switch type on cable edit

* 15973 fix cable add from device
Arthur Hanson 1 سال پیش
والد
کامیت
a953ff20f9
1فایلهای تغییر یافته به همراه16 افزوده شده و 7 حذف شده
  1. 16 7
      netbox/dcim/forms/connections.py

+ 16 - 7
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()