Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15973 fix switch type on cable edit #16049

Merged
merged 2 commits into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions netbox/dcim/forms/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ def __new__(mcs, name, bases, attrs):
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'):
Expand All @@ -108,8 +108,17 @@ def __init__(self, *args, initial=None, **kwargs):

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()
Expand Down
Loading