Blob Blame History Raw
From b140c16ca8f9ed0227f5295878c3f6b346a8472c Mon Sep 17 00:00:00 2001
From: David Szotten <davidszotten@gmail.com>
Date: Wed, 19 Jan 2022 17:53:07 +0000
Subject: [PATCH 1/5] failing test to expose issue

---
 tests/test_models.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tests/test_models.py b/tests/test_models.py
index fb5b55cc..0ae6ba4e 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -5,6 +5,7 @@
 """
 import datetime
 from copy import copy
+from decimal import Decimal
 
 from django import VERSION
 from django.core.exceptions import ValidationError
@@ -373,6 +374,12 @@ def test_fails_with_null_currency(self):
         assert str(exc.value) == "Missing currency value"
         assert not ModelWithNullableCurrency.objects.exists()
 
+    def test_fails_with_null_currency_decimal(self):
+        with pytest.raises(ValueError) as exc:
+            ModelWithNullableCurrency.objects.create(money=Decimal(10))
+        assert str(exc.value) == "Missing currency value"
+        assert not ModelWithNullableCurrency.objects.exists()
+
     def test_fails_with_nullable_but_no_default(self):
         with pytest.raises(IntegrityError) as exc:
             ModelWithTwoMoneyFields.objects.create()

From 2ccaadc4e1d3a7ca06ba96ee683fb9057daa8d94 Mon Sep 17 00:00:00 2001
From: David Szotten <davidszotten@gmail.com>
Date: Wed, 19 Jan 2022 17:54:30 +0000
Subject: [PATCH 2/5] suggested better fix for
 https://github.com/django-money/django-money/pull/427/

TODO: is the currency field guaranteed to appear before the amount (and main)
field? yes because of the creation_counters?
---
 djmoney/models/fields.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/djmoney/models/fields.py b/djmoney/models/fields.py
index 8eb710ba..3bfd7dda 100644
--- a/djmoney/models/fields.py
+++ b/djmoney/models/fields.py
@@ -104,7 +104,12 @@ def __get__(self, obj, type=None):
         return data[self.field.name]
 
     def __set__(self, obj, value):  # noqa
-        if value is not None and self.field._currency_field.null and not isinstance(value, MONEY_CLASSES + (Decimal,)):
+        if (
+            value is not None
+            and self.field._currency_field.null
+            and not isinstance(value, MONEY_CLASSES)
+            and not obj.__dict__[self.currency_field_name]
+        ):
             # For nullable fields we need either both NULL amount and currency or both NOT NULL
             raise ValueError("Missing currency value")
         if isinstance(value, BaseExpression):

From 952ac5a75b43a632febe733d0aa1a5a716b7735c Mon Sep 17 00:00:00 2001
From: David Szotten <davidszotten@gmail.com>
Date: Wed, 19 Jan 2022 22:09:06 +0000
Subject: [PATCH 3/5] fix django error message change

fix for
https://github.com/django/django/commit/08d8bccbf1b0764a0de68325569ee47da256e206
---
 tests/test_models.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tests/test_models.py b/tests/test_models.py
index 0ae6ba4e..53def7cb 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -719,7 +719,9 @@ def test_override_decorator():
 def test_properties_access():
     with pytest.raises(TypeError) as exc:
         ModelWithVanillaMoneyField(money=Money(1, "USD"), bla=1)
-    if VERSION[:2] > (2, 1):
+    if VERSION[:2] > (4, 0):
+        assert str(exc.value) == "ModelWithVanillaMoneyField() got unexpected keyword arguments: 'bla'"
+    elif VERSION[:2] > (2, 1):
         assert str(exc.value) == "ModelWithVanillaMoneyField() got an unexpected keyword argument 'bla'"
     else:
         assert str(exc.value) == "'bla' is an invalid keyword argument for this function"

From 620af5355a22ca1da0cb43c8e5787c3e8b76f995 Mon Sep 17 00:00:00 2001
From: David Szotten <davidszotten@gmail.com>
Date: Thu, 20 Jan 2022 08:59:37 +0000
Subject: [PATCH 4/5] better match the signature of input()

for compat with django 0ab58c120939093fea90822f376e1866fc714d1f
---
 tests/migrations/helpers.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/migrations/helpers.py b/tests/migrations/helpers.py
index f38067fe..cbf2c7fe 100644
--- a/tests/migrations/helpers.py
+++ b/tests/migrations/helpers.py
@@ -12,7 +12,7 @@ def makemigrations():
     from django.db.migrations import questioner
 
     # We should answer yes for all migrations questioner questions
-    questioner.input = lambda x: "y"
+    questioner.input = lambda prompt=None: "y"
 
     os.system("find . -name \\*.pyc -delete")
     call_command("makemigrations", "money_app", name=MIGRATION_NAME)

From faf4da5f96193fc1a5e0b2b838f2a13189975abf Mon Sep 17 00:00:00 2001
From: David Szotten <davidszotten@gmail.com>
Date: Thu, 27 Jan 2022 17:47:01 +0000
Subject: [PATCH 5/5] changelog

---
 docs/changes.rst | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/docs/changes.rst b/docs/changes.rst
index 1968af75..5eab8fd1 100644
--- a/docs/changes.rst
+++ b/docs/changes.rst
@@ -1,6 +1,14 @@
 Changelog
 =========
 
+`Unreleased`_ - TBA
+-------------------
+
+**Fixed**
+
+- Improve the internal check for whether a currency is provided `#657`_ (`davidszotten`_)
+- Fix test suite for django main branch `#657`_ (`davidszotten`_)
+
 `2.1.1`_ - 2022-01-02
 ---------------------
 
@@ -709,8 +717,7 @@ wrapping with ``money_manager``.
 
 - Initial public release
 
-# .. _Unreleased: https:///github.com/django-money/django-money/compare/2.1.1...HEAD
-
+.. _Unreleased: https:///github.com/django-money/django-money/compare/2.1.1...HEAD
 .. _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
 .. _2.0.3: https://github.com/django-money/django-money/compare/2.0.2...2.0.3
@@ -773,6 +780,7 @@ wrapping with ``money_manager``.
 .. _0.3: https://github.com/django-money/django-money/compare/0.2...0.3
 .. _0.2: https://github.com/django-money/django-money/compare/0.2...a6d90348085332a393abb40b86b5dd9505489b04
 
+.. _#657: https://github.com/django-money/django-money/issues/657
 .. _#648: https://github.com/django-money/django-money/issues/648
 .. _#646: https://github.com/django-money/django-money/issues/646
 .. _#637: https://github.com/django-money/django-money/issues/637
@@ -966,3 +974,4 @@ wrapping with ``money_manager``.
 .. _washeck: https://github.com/washeck
 .. _fara: https://github.com/fara
 .. _wearebasti: https://github.com/wearebasti
+.. _davidszotten: https://github.com/davidszotten