Skip to content

Commit

Permalink
Fix a bug in serialized_data with F expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Jul 12, 2023
1 parent 875c6b7 commit faafb71
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- fix: Make sure `LogEntry.changes_dict()` returns an empty dict instead of `None` when `json.loads()` returns `None`. ([#472](https://github.com/jazzband/django-auditlog/pull/472))
- 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)

Expand Down
5 changes: 4 additions & 1 deletion auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ def _get_copy_with_python_typed_fields(self, instance):
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 Exception as e:
continue
return instance_copy

def _get_applicable_model_fields(
Expand Down
19 changes: 19 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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 reverse
Expand Down Expand Up @@ -2244,6 +2245,24 @@ def test_serialize_related_with_kwargs(self):
},
)

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):
Expand Down

0 comments on commit faafb71

Please sign in to comment.