mirror of
https://github.com/django/django.git
synced 2025-07-07 21:35:15 +00:00
Fixed #36453 -- Made When.condition resolve with for_save=False.
Some checks failed
Linters / flake8 (push) Waiting to run
Linters / isort (push) Waiting to run
Linters / black (push) Waiting to run
Tests / Windows, SQLite, Python 3.13 (push) Waiting to run
Tests / JavaScript tests (push) Waiting to run
Docs / docs (push) Has been cancelled
Docs / blacken-docs (push) Has been cancelled
Some checks failed
Linters / flake8 (push) Waiting to run
Linters / isort (push) Waiting to run
Linters / black (push) Waiting to run
Tests / Windows, SQLite, Python 3.13 (push) Waiting to run
Tests / JavaScript tests (push) Waiting to run
Docs / docs (push) Has been cancelled
Docs / blacken-docs (push) Has been cancelled
Value(None, JSONField()) when used in When.condition incorrectly resolved with
for_save=True, resulting in the value being serialized as SQL NULL instead of
JSON null.
Regression in c1fa3fdd04
.
Thanks to Thomas McKay for the report, and to David Sanders and Simon Charettes
for the review.
Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
This commit is contained in:
parent
12c1557060
commit
104cbfd44b
3 changed files with 32 additions and 0 deletions
|
@ -1620,6 +1620,17 @@ class When(Expression):
|
|||
def set_source_expressions(self, exprs):
|
||||
self.condition, self.result = exprs
|
||||
|
||||
def resolve_expression(
|
||||
self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
|
||||
):
|
||||
c = super().resolve_expression(query, allow_joins, reuse, summarize, for_save)
|
||||
if for_save and c.condition is not None:
|
||||
# Resolve condition with for_save=False, since it's used as a filter.
|
||||
c.condition = self.condition.resolve_expression(
|
||||
query, allow_joins, reuse, summarize, for_save=False
|
||||
)
|
||||
return c
|
||||
|
||||
def get_source_fields(self):
|
||||
# We're only interested in the fields of the result expressions.
|
||||
return [self.result._output_field_or_none]
|
||||
|
|
|
@ -12,3 +12,7 @@ Bugfixes
|
|||
* Fixed a regression in Django 5.2.2 where :meth:`HttpRequest.get_preferred_type()
|
||||
<django.http.HttpRequest.get_preferred_type>` incorrectly preferred more
|
||||
specific media types with a lower quality (:ticket:`36447`).
|
||||
|
||||
* Fixed a regression in Django 5.2.3 where ``Value(None, JSONField())`` used in
|
||||
a :class:`~django.db.models.expressions.When` condition was incorrectly
|
||||
serialized as SQL ``NULL`` instead of JSON ``null`` (:ticket:`36453`).
|
||||
|
|
|
@ -29,6 +29,7 @@ from django.db.models import (
|
|||
FloatField,
|
||||
Func,
|
||||
IntegerField,
|
||||
JSONField,
|
||||
Max,
|
||||
Min,
|
||||
Model,
|
||||
|
@ -83,6 +84,7 @@ from .models import (
|
|||
Company,
|
||||
Employee,
|
||||
Experiment,
|
||||
JSONFieldModel,
|
||||
Manager,
|
||||
Number,
|
||||
RemoteEmployee,
|
||||
|
@ -367,6 +369,21 @@ class BasicExpressionsTests(TestCase):
|
|||
Number.objects.all(), [None, None], lambda n: n.float, ordered=False
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature("supports_json_field")
|
||||
def test_update_jsonfield_case_when_key_is_null(self):
|
||||
obj = JSONFieldModel.objects.create(data={"key": None})
|
||||
updated = JSONFieldModel.objects.update(
|
||||
data=Case(
|
||||
When(
|
||||
data__key=Value(None, JSONField()),
|
||||
then=Value({"key": "something"}, JSONField()),
|
||||
),
|
||||
)
|
||||
)
|
||||
self.assertEqual(updated, 1)
|
||||
obj.refresh_from_db()
|
||||
self.assertEqual(obj.data, {"key": "something"})
|
||||
|
||||
def test_filter_with_join(self):
|
||||
# F Expressions can also span joins
|
||||
Company.objects.update(point_of_contact=F("ceo"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue