From 2b3b9517d257212bc0a3862c93f0a24428d426e9 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 20 Mar 2023 15:40:49 -0400 Subject: [PATCH] #9073: Fix form behavior when disassociating a ConfigContext from a DataFile --- netbox/extras/forms/model_forms.py | 8 ++++++++ netbox/netbox/models/features.py | 15 +++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index 2a3adf790..86cd457b3 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -267,6 +267,14 @@ class ConfigContextForm(BootstrapMixin, SyncedDataMixin, forms.ModelForm): 'tenants', 'tags', 'data_source', 'data_file', ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Disable data field when a DataFile has been set + if self.instance.data_file: + self.fields['data'].widget.attrs['readonly'] = True + self.fields['data'].help_text = _('Data is populated from the remote source selected below.') + def clean(self): super().clean() diff --git a/netbox/netbox/models/features.py b/netbox/netbox/models/features.py index 346121823..0e4b37125 100644 --- a/netbox/netbox/models/features.py +++ b/netbox/netbox/models/features.py @@ -372,16 +372,15 @@ class SyncedDataMixin(models.Model): return self.data_file and self.data_synced >= self.data_file.last_updated def clean(self): - if self.data_file: - self.sync_data() - self.data_path = self.data_file.path - if self.data_source and not self.data_file: - raise ValidationError({ - 'data_file': _(f"Must specify a data file when designating a data source.") - }) - if self.data_file and not self.data_source: + if self.data_file: self.data_source = self.data_file.source + self.data_path = self.data_file.path + self.sync_data() + else: + self.data_source = None + self.data_path = '' + self.data_synced = None super().clean()