Fix repr of a json field in field changes (#489)

This commit is contained in:
Cleiton de Lima 2022-12-30 04:28:55 -03:00 committed by GitHub
parent 7f8edd5456
commit ffa6d34b11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 2 deletions

View file

@ -8,6 +8,7 @@
#### Improvements
- Changes the view when it has changes in fields `JSONField`. The `JSONField.encoder` is assigned to `json.dumps`. ([#489](https://github.com/jazzband/django-auditlog/pull/489))
- feat: Added support for Correlation ID. ([#481](https://github.com/jazzband/django-auditlog/pull/481))
- feat: Added pre-log and post-log signals. ([#483](https://github.com/jazzband/django-auditlog/pull/483))

View file

@ -1,3 +1,4 @@
import json
from datetime import timezone
from typing import Optional
@ -74,6 +75,7 @@ def get_field_value(obj, field):
value = django_timezone.make_naive(value, timezone=timezone.utc)
elif isinstance(field, JSONField):
value = field.to_python(getattr(obj, field.name, None))
value = json.dumps(value, sort_keys=True, cls=field.encoder)
elif (field.one_to_one or field.many_to_one) and hasattr(field, "rel_class"):
value = smart_str(
getattr(obj, field.get_attname(), None), strings_only=True

View file

@ -1,6 +1,7 @@
import uuid
from django.contrib.postgres.fields import ArrayField
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from auditlog.models import AuditlogHistoryField
@ -260,7 +261,7 @@ class NoDeleteHistoryModel(models.Model):
class JSONModel(models.Model):
json = models.JSONField(default=dict)
json = models.JSONField(default=dict, encoder=DjangoJSONEncoder)
history = AuditlogHistoryField(delete_related=False)

View file

@ -1601,7 +1601,7 @@ class JSONModelTest(TestCase):
self.assertDictEqual(
history.changes,
{"json": ["{}", "{'quantity': '1'}"]},
{"json": ["{}", '{"quantity": "1"}']},
msg="The change is correctly logged",
)
@ -1693,6 +1693,34 @@ class ModelInstanceDiffTest(TestCase):
msg="ObjectDoesNotExist should be handled",
)
def test_diff_models_with_json_fields(self):
first = JSONModel.objects.create(
json={
"code": "17",
"date": datetime.date(2022, 1, 1),
"description": "first",
}
)
first.refresh_from_db() # refresh json data from db
second = JSONModel.objects.create(
json={
"code": "17",
"description": "second",
"date": datetime.date(2023, 1, 1),
}
)
diff = model_instance_diff(first, second, ["json"])
self.assertDictEqual(
diff,
{
"json": (
'{"code": "17", "date": "2022-01-01", "description": "first"}',
'{"code": "17", "date": "2023-01-01", "description": "second"}',
)
},
)
class TestRelatedDiffs(TestCase):
def setUp(self):