Fix serialize __proxy__ objects before logging (#624)

* Fix serialize __proxy__ objects before logging

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update CHANGELOG.md

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
This commit is contained in:
nathan 2024-04-08 12:06:36 +02:00 committed by GitHub
parent 2f949dde25
commit a93d9ef665
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 1 deletions

View file

@ -1,6 +1,9 @@
# Changes
## Next Release
#### Fixes
- Fixed logging problem related to django translation before logging ([#624](https://github.com/jazzband/django-auditlog/pull/624))
## 3.0.0-beta.4 (2024-01-02)

View file

@ -82,6 +82,8 @@ def get_field_value(obj, field):
)
else:
value = smart_str(getattr(obj, field.name, None))
if type(value).__name__ == "__proxy__":
value = str(value)
except ObjectDoesNotExist:
value = (
field.default

View file

@ -24,7 +24,7 @@ class SimpleModel(models.Model):
history = AuditlogHistoryField(delete_related=True)
def __str__(self):
return self.text
return str(self.text)
class AltPrimaryKeyModel(models.Model):

View file

@ -23,6 +23,7 @@ from django.urls import resolve, reverse
from django.utils import dateformat, formats
from django.utils import timezone as django_timezone
from django.utils.encoding import smart_str
from django.utils.translation import gettext_lazy as _
from auditlog.admin import LogEntryAdmin
from auditlog.cid import get_cid
@ -1641,6 +1642,15 @@ class DiffMsgTest(TestCase):
),
)
def test_instance_translation_and_history_logging(self):
first = SimpleModel()
second = SimpleModel(text=_("test"))
changes = model_instance_diff(first, second)
self.assertEqual(changes, {"text": ("", "test")})
second.save()
log_one = second.history.last()
self.assertTrue(isinstance(log_one, LogEntry))
def test_changes_msg_create(self):
log_entry = self._create_log_entry(
LogEntry.Action.CREATE,