Make M2M changes comply with JSONField properly (#514)

This commit is contained in:
Joey Lange 2023-02-15 08:10:27 -06:00 committed by GitHub
parent fcb6c4ce27
commit 98fe8d4173
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View file

@ -126,15 +126,14 @@ class LogEntryManager(models.Manager):
kwargs.setdefault("additional_data", get_additional_data())
objects = [smart_str(instance) for instance in changed_queryset]
kwargs["changes"] = json.dumps(
{
field_name: {
"type": "m2m",
"operation": operation,
"objects": objects,
}
kwargs["changes"] = {
field_name: {
"type": "m2m",
"operation": operation,
"objects": objects,
}
)
}
kwargs.setdefault("cid", get_cid())
return self.create(**kwargs)

View file

@ -21,6 +21,7 @@ from django.test import RequestFactory, TestCase, override_settings
from django.urls import reverse
from django.utils import dateformat, formats
from django.utils import timezone as django_timezone
from django.utils.encoding import smart_str
from auditlog.admin import LogEntryAdmin
from auditlog.cid import get_cid
@ -400,6 +401,20 @@ class ManyRelatedModelTest(TestCase):
log_entry.additional_data, {"related_model_id": self.related.id}
)
def test_changes(self):
self.obj.related.add(self.related)
log_entry = self.obj.history.first()
self.assertEqual(
log_entry.changes,
{
"related": {
"type": "m2m",
"operation": "add",
"objects": [smart_str(self.related)],
}
},
)
class MiddlewareTest(TestCase):
"""