Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempts to fix update_change_reason for nullable JSONField fields (issue #1181) #1282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion simple_history/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from django.db import transaction
from django.db.models import Case, ForeignKey, ManyToManyField, Q, When
from django.db.models import (
Case,
ForeignKey,
JSONField,
ManyToManyField,
Q,
Value,
When,
)
from django.forms.models import model_to_dict

from simple_history.exceptions import AlternativeManagerError, NotHistoricalModelError
Expand All @@ -18,6 +26,11 @@
if field.primary_key is True:
if value is not None:
attrs[field.attname] = value
elif isinstance(field, JSONField):
if value is None and field.null is True:
attrs[f"{field.attname}__isnull"] = True

Check warning on line 31 in simple_history/utils.py

View check run for this annotation

Codecov / codecov/patch

simple_history/utils.py#L31

Added line #L31 was not covered by tests
else:
attrs[field.attname] = Value(value, JSONField())

Check warning on line 33 in simple_history/utils.py

View check run for this annotation

Codecov / codecov/patch

simple_history/utils.py#L33

Added line #L33 was not covered by tests
else:
attrs[field.attname] = value

Expand Down