diff --git a/app/admin/utils.py b/app/admin/utils.py index 7444dc4..c07b3a4 100644 --- a/app/admin/utils.py +++ b/app/admin/utils.py @@ -51,5 +51,21 @@ def log_admin_action(action, target_type=None, target_id=None, before=None, afte def model_to_dict(obj, fields): - """Return a plain dict of the named fields from an ORM object (for audit before/after).""" - return {f: getattr(obj, f, None) for f in fields} + """ + Return a plain JSON-serialisable dict of the named fields from an ORM object. + Converts datetime -> ISO-8601 string, Decimal -> float, all others pass through. + Safe to use directly as AuditLog before_json / after_json values. + """ + import datetime + from decimal import Decimal + + result = {} + for f in fields: + val = getattr(obj, f, None) + if isinstance(val, (datetime.datetime, datetime.date)): + result[f] = val.isoformat() + elif isinstance(val, Decimal): + result[f] = float(val) + else: + result[f] = val + return result \ No newline at end of file