From c0e6f6172f4813d5d6766c2ca8f02b663a4322c0 Mon Sep 17 00:00:00 2001 From: Arthur Date: Thu, 14 Dec 2023 10:35:26 -0800 Subject: [PATCH] 14147 add exclude_fields to serialize_object --- netbox/netbox/models/features.py | 16 ++++++++++++---- netbox/utilities/utils.py | 8 +++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/netbox/netbox/models/features.py b/netbox/netbox/models/features.py index 3c4806c1d..571327fe4 100644 --- a/netbox/netbox/models/features.py +++ b/netbox/netbox/models/features.py @@ -64,19 +64,23 @@ class ChangeLoggingMixin(models.Model): class Meta: abstract = True - def serialize_object(self): + def serialize_object(self, exclude_fields=[]): """ Return a JSON representation of the instance. Models can override this method to replace or extend the default serialization logic provided by the `serialize_object()` utility function. """ - return serialize_object(self) + return serialize_object(self, exclude_fields=exclude_fields) def snapshot(self): """ Save a snapshot of the object's current state in preparation for modification. The snapshot is saved as `_prechange_snapshot` on the instance. """ - self._prechange_snapshot = self.serialize_object() + exclude_fields = [] + if get_config().CHANGELOG_SKIP_EMPTY_CHANGES: + exclude_fields = ['last_updated',] + + self._prechange_snapshot = self.serialize_object(exclude_fields=exclude_fields) snapshot.alters_data = True def to_objectchange(self, action): @@ -86,9 +90,13 @@ class ChangeLoggingMixin(models.Model): """ from extras.models import ObjectChange + exclude_fields = [] + if get_config().CHANGELOG_SKIP_EMPTY_CHANGES: + exclude_fields = ['last_updated',] + postchange_data = None if action in (ObjectChangeActionChoices.ACTION_CREATE, ObjectChangeActionChoices.ACTION_UPDATE): - postchange_data = self.serialize_object() + postchange_data = self.serialize_object(exclude_fields=exclude_fields) if get_config().CHANGELOG_SKIP_EMPTY_CHANGES and action == ObjectChangeActionChoices.ACTION_UPDATE and hasattr(self, '_prechange_snapshot'): if postchange_data == self._prechange_snapshot: diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index f7ad8f708..d79abfd3b 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -144,7 +144,7 @@ def count_related(model, field): return Coalesce(subquery, 0) -def serialize_object(obj, resolve_tags=True, extra=None): +def serialize_object(obj, resolve_tags=True, extra=None, exclude_fields=[]): """ Return a generic JSON representation of an object using Django's built-in serializer. (This is used for things like change logging, not the REST API.) Optionally include a dictionary to supplement the object data. A list of keys @@ -159,8 +159,10 @@ def serialize_object(obj, resolve_tags=True, extra=None): for field in ['level', 'lft', 'rght', 'tree_id']: data.pop(field) - if get_config().CHANGELOG_SKIP_EMPTY_CHANGES and 'last_updated' in data: - data.pop('last_updated') + if exclude_fields: + for field in exclude_fields: + if field in data: + data.pop(field) # Include custom_field_data as "custom_fields" if hasattr(obj, 'custom_field_data'):