Skip to content

Commit

Permalink
Fixed a problem when setting Value(None) in JSONField
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed May 22, 2024
1 parent ba2c2e3 commit fe2dea9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#### Fixes

- Fixed problem when setting `django.db.models.functions.Now()` in `DateTimeField` ([#635](https://github.com/jazzband/django-auditlog/pull/635))
- Fixed a problem when setting `Value(None)` in `JSONField` ([#646](https://github.com/jazzband/django-auditlog/pull/646))
- Fixed a problem when setting `django.db.models.functions.Now()` in `DateTimeField` ([#635](https://github.com/jazzband/django-auditlog/pull/635))

## 3.0.0 (2024-04-12)

Expand Down
5 changes: 4 additions & 1 deletion auditlog/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ 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)
try:
value = json.dumps(value, sort_keys=True, cls=field.encoder)
except TypeError:
pass
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
Expand Down
9 changes: 9 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core import management
from django.db import models
from django.db.models import JSONField, Value
from django.db.models.functions import Now
from django.db.models.signals import pre_save
from django.test import RequestFactory, TestCase, override_settings
Expand Down Expand Up @@ -1090,6 +1091,14 @@ def test_datetime_field_functions_now(self):
dtm.save()
self.assertEqual(dtm.naive_dt, Now())

def test_json_field_value_none(self):
json_model = JSONModel(json=Value(None, JSONField()))
json_model.save()
self.assertEqual(json_model.history.count(), 1)
self.assertEqual(
json_model.history.latest().changes_dict["json"][1], 'Value(None)'
)


class UnregisterTest(TestCase):
def setUp(self):
Expand Down

0 comments on commit fe2dea9

Please sign in to comment.