1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[5.2.x] Fixed #36419 -- Ensured for_save was propagated when resolving expressions.

The for_save flag wasn't properly propagated when resolving expressions, which
prevented get_db_prep_save() from being called in some cases. This affected
fields like JSONField where None would be saved as JSON null instead of SQL NULL.

Regression in 00c690efbc.

Thanks to David Sanders and Simon Charette for reviews.

Co-authored-by: Adam Johnson <me@adamj.eu>

Backport of c1fa3fdd04 from main.
This commit is contained in:
Clifford Gama
2025-05-26 16:44:08 +02:00
committed by Sarah Boyce
parent 8fcc83953c
commit 6fc620b4a8
3 changed files with 21 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ import datetime
from django.core.exceptions import FieldDoesNotExist
from django.db.models import F
from django.db.models.functions import Lower
from django.db.models.functions import Coalesce, Lower
from django.db.utils import IntegrityError
from django.test import TestCase, override_settings, skipUnlessDBFeature
@@ -289,6 +289,21 @@ class BulkUpdateTests(TestCase):
JSONFieldNullable.objects.filter(json_field__has_key="c"), objs
)
@skipUnlessDBFeature("supports_json_field")
def test_json_field_sql_null(self):
obj = JSONFieldNullable.objects.create(json_field={})
test_cases = [
("direct_none_assignment", None),
("expression_none_assignment", Coalesce(None, None)),
]
for label, value in test_cases:
with self.subTest(case=label):
obj.json_field = value
JSONFieldNullable.objects.bulk_update([obj], fields=["json_field"])
obj.refresh_from_db()
sql_null_qs = JSONFieldNullable.objects.filter(json_field__isnull=True)
self.assertSequenceEqual(sql_null_qs, [obj])
def test_nullable_fk_after_related_save(self):
parent = RelatedObject.objects.create()
child = SingleObject()