1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Further work on custom fields

This commit is contained in:
Jeremy Stretch
2020-08-24 14:11:13 -04:00
parent 2276603ac3
commit c85a45e520
4 changed files with 67 additions and 92 deletions

View File

@ -57,26 +57,13 @@ class CustomFieldModelForm(forms.ModelForm):
# Annotate the field in the list of CustomField form fields
self.custom_fields.append(field_name)
def _save_custom_fields(self):
for field_name in self.custom_fields:
self.instance.custom_field_data[field_name[3:]] = self.cleaned_data[field_name]
def save(self, commit=True):
# Cache custom field values on object prior to save to ensure change logging
# Save custom field data on instance
for cf_name in self.custom_fields:
self.instance._cf[cf_name[3:]] = self.cleaned_data.get(cf_name)
self.instance.custom_field_data[cf_name[3:]] = self.cleaned_data.get(cf_name)
obj = super().save(commit)
# Handle custom fields the same way we do M2M fields
if commit:
self._save_custom_fields()
else:
obj.save_custom_fields = self._save_custom_fields
return obj
return super().save(commit)
class CustomFieldModelCSVForm(CSVModelForm, CustomFieldModelForm):