Skip to content

Commit 6f1bb68

Browse files
committed
added support for custom json object serializer
1 parent 03e05bc commit 6f1bb68

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

easyaudit/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,6 @@ def get_model_list(class_list):
103103
CRUD_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_EVENT_LIST_FILTER', ['event_type', 'content_type', 'user', 'datetime', ])
104104
LOGIN_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_LOGIN_EVENT_LIST_FILTER', ['login_type', 'user', 'datetime', ])
105105
REQUEST_EVENT_LIST_FILTER = getattr(settings, 'DJANGO_EASY_AUDIT_REQUEST_EVENT_LIST_FILTER', ['method', 'user', 'datetime', ])
106+
107+
# JSON object representation serializer override
108+
CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE = getattr(settings, 'DJANGO_EASY_AUDIT_CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE', None)

easyaudit/signals/model_signals.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
get_current_user
1515
from easyaudit.models import CRUDEvent
1616
from easyaudit.settings import REGISTERED_CLASSES, UNREGISTERED_CLASSES, \
17-
WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS
17+
WATCH_MODEL_EVENTS, CRUD_DIFFERENCE_CALLBACKS,\
18+
CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE
1819
from easyaudit.utils import model_delta
1920

2021
logger = logging.getLogger(__name__)
@@ -53,7 +54,10 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
5354
if not should_audit(instance):
5455
return False
5556
try:
56-
object_json_repr = serializers.serialize("json", [instance])
57+
if CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE != None:
58+
object_json_repr = CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE(instance)
59+
else:
60+
object_json_repr = serializers.serialize("json", [instance])
5761
except Exception:
5862
# We need a better way for this to work. ManyToMany will fail on pre_save on create
5963
return None
@@ -122,7 +126,11 @@ def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
122126
with transaction.atomic():
123127
if not should_audit(instance):
124128
return False
125-
object_json_repr = serializers.serialize("json", [instance])
129+
130+
if CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE != None:
131+
object_json_repr = CRUD_OBJECT_JSON_REPR_SERIALIZER_OVERRIDE(instance)
132+
else:
133+
object_json_repr = serializers.serialize("json", [instance])
126134

127135
# created or updated?
128136
if created:

0 commit comments

Comments
 (0)