Clean CircuitTerminationForm data to avoid model ValidationErrors

Associated with issue #11891
This commit is contained in:
Dillon Henschen 2023-05-25 20:54:39 -04:00
parent dffb6fa329
commit d35acf0a01

View File

@ -169,3 +169,17 @@ class CircuitTerminationForm(NetBoxModelForm):
labels = {
'termination_type': 'Termination Type',
}
def clean(self):
super().clean()
termination_type = self.cleaned_data.get('termination_type')
# Clear out the unselected termination type fields to prevent a ValidationError in the model.
# Otherwise, users would have to manually clear out the site or provider network field, before
# changing the termination type. That behavior may not be obvious or convenient for most users.
if termination_type != CircuitTerminationTypeChoices.SITE:
self.cleaned_data['site'] = None
if termination_type != CircuitTerminationTypeChoices.PROVIDER_NETWORK:
self.cleaned_data['provider_network'] = None