Skip to content

Commit

Permalink
Handle Coalesce in updates
Browse files Browse the repository at this point in the history
Modeled on #534, which
I see came out of a similar issue as I've been seeing now:
"AttributeError: 'Coalesce' object has no attribute 'rhs'
  • Loading branch information
stianjensen authored and Stranger6667 committed Oct 28, 2022
1 parent fe9b71a commit 667342f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions djmoney/models/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db.models import NOT_PROVIDED, Case, F, Q
from django.db.models.constants import LOOKUP_SEP
from django.db.models.expressions import BaseExpression
from django.db.models.functions import Cast
from django.db.models.functions import Cast, Coalesce
from django.utils.encoding import smart_str

from ..utils import MONEY_CLASSES, get_currency_field_name, prepare_expression
Expand Down Expand Up @@ -142,7 +142,7 @@ def _expand_money_kwargs(model, args=(), kwargs=None, exclusions=()):
kwargs[currency_field_name] = smart_str(value.currency)
else:
if isinstance(field, MoneyField):
if isinstance(value, (BaseExpression, F)) and not isinstance(value, (Case, Cast)):
if isinstance(value, (BaseExpression, F)) and not isinstance(value, (Case, Cast, Coalesce)):
clean_name = _get_clean_name(model, name)
if not isinstance(value, F):
value = prepare_expression(value)
Expand Down
10 changes: 10 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

`Unreleased`_ - TBA
-------------------

**Fixed**

- Support for ``Money`` type with ``Coalesce`` in ``QuerySet.update()`` `#678`_ (`stianjensen`_)


`3.0`_ - 2022-06-20
--------------------
Expand Down Expand Up @@ -737,6 +744,7 @@ wrapping with ``money_manager``.

- Initial public release

.. _Unreleased: https:///github.com/django-money/django-money/compare/3.0...HEAD
.. _3.0: https:///github.com/django-money/django-money/compare/2.1.1...3.0
.. _2.1.1: https:///github.com/django-money/django-money/compare/2.1...2.1.1
.. _2.1: https:///github.com/django-money/django-money/compare/2.0.3...2.1
Expand Down Expand Up @@ -915,6 +923,7 @@ wrapping with ``money_manager``.
.. _#519: https://github.com/django-money/django-money/issues/519
.. _#521: https://github.com/django-money/django-money/issues/521
.. _#522: https://github.com/django-money/django-money/issues/522
.. _#678: https://github.com/django-money/django-money/pull/678


.. _77cc33: https://github.com/77cc33
Expand Down Expand Up @@ -983,6 +992,7 @@ wrapping with ``money_manager``.
.. _sjdines: https://github.com/sjdines
.. _snbuchholz: https://github.com/snbuchholz
.. _spookylukey: https://github.com/spookylukey
.. _stianjensen: https://github.com/stianjensen
.. _stinovlas: https://github.com/stinovlas
.. _synotna: https://github.com/synotna
.. _tned73: https://github.com/tned73
Expand Down
9 changes: 9 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.db import IntegrityError, models
from django.db.migrations.writer import MigrationWriter
from django.db.models import Case, F, Func, Q, Value, When
from django.db.models.functions import Coalesce
from django.utils.translation import override

import pytest
Expand Down Expand Up @@ -624,6 +625,14 @@ def test_conditional_update(self):
assert ModelWithVanillaMoneyField.objects.get(integer=0).money == Money(10, "USD")
assert ModelWithVanillaMoneyField.objects.get(integer=1).money == Money(0, "USD")

def test_update_with_coalesce(self):
ModelWithVanillaMoneyField.objects.create(money=Money(1, "USD"), second_money=Money(2, "USD"), integer=0)
ModelWithVanillaMoneyField.objects.update(
money=Coalesce(F("second_money"), 0, output_field=models.DecimalField())
)
instance = ModelWithVanillaMoneyField.objects.get()
assert instance.money == Money("2", "USD")

def test_create_func(self):
instance = ModelWithVanillaMoneyField.objects.create(
money=Func(Value(-10), function="ABS"), money_currency="USD"
Expand Down

0 comments on commit 667342f

Please sign in to comment.