Fix a bug in serialized_data with F expressions (#544)

This commit is contained in:
Hasan Ramezani 2023-07-13 08:53:47 +02:00 committed by Ilya
parent 188337b465
commit 78dc7a5012
3 changed files with 24 additions and 1 deletions

View file

@ -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)

View file

@ -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(

View file

@ -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):