diff --git a/CHANGELOG.md b/CHANGELOG.md index 663ed60..b954178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ ~~- fix: revert [#449](https://github.com/jazzband/django-auditlog/pull/449) "Make log entries read-only in the admin" as it breaks deletion of any auditlogged model through the admin when `AuditlogHistoryField` is used. ([#496](https://github.com/jazzband/django-auditlog/pull/496))~~ - fix: Always set remote_addr even if the request has no authenticated user. ([#484](https://github.com/jazzband/django-auditlog/pull/484)) - fix: Fix a bug in getting field's `verbose_name` when model is not accessible. ([508](https://github.com/jazzband/django-auditlog/pull/508)) +- fix: Fix a bug in `serialized_data` with F expressions. ([508](https://github.com/jazzband/django-auditlog/pull/508)) ## 2.2.2 (2023-01-16) diff --git a/auditlog/models.py b/auditlog/models.py index 933b09e..1f2645b 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -249,7 +249,10 @@ class LogEntryManager(models.Manager): for field in instance_copy._meta.fields: if not field.is_relation: value = getattr(instance_copy, field.name) - setattr(instance_copy, field.name, field.to_python(value)) + try: + setattr(instance_copy, field.name, field.to_python(value)) + except ValidationError: + continue return instance_copy def _get_applicable_model_fields( diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index a79568a..75a4e0a 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -16,6 +16,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import AnonymousUser, User from django.contrib.contenttypes.models import ContentType from django.core import management +from django.db import models from django.db.models.signals import pre_save from django.test import RequestFactory, TestCase, override_settings from django.urls import resolve, reverse @@ -2087,6 +2088,24 @@ class TestModelSerialization(TestCase): }, ) + def test_f_expressions(self): + serialize_this = SerializeThisModel.objects.create( + label="test label", + nested={"foo": "bar"}, + timestamp=self.test_date, + nullable=1, + ) + serialize_this.nullable = models.F("nullable") + 1 + serialize_this.save() + + log = serialize_this.history.first() + self.assertTrue(isinstance(log, LogEntry)) + self.assertEqual(log.action, 1) + self.assertEqual( + log.serialized_data["fields"]["nullable"], + "F(nullable) + Value(1)", + ) + class TestAccessLog(TestCase): def setUp(self):