mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-18 05:28:16 -06:00
14147 add exclude_fields to serialize_object
This commit is contained in:
parent
68232511a0
commit
c0e6f6172f
@ -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:
|
||||
|
@ -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'):
|
||||
|
Loading…
Reference in New Issue
Block a user