Repository: django-money/django-money

Issue:
default_currency=None keeps detecting migration changes
Based on the comment in the discussion found here: https://github.com/django-money/django-money/issues/530#issuecomment-650166087

If `MoneyField(..., default_currency=None)` is added to an already existing model, so that the field is triggering migration operations `AddField` or `AlterField`. Django keeps detecting changes on the field/model when running `python manage.py makemigrations`.

Discussion:
flaeppe: With a quick peek through the code I couldn't find any other tests in regards to `MoneyField.deconstruct`. Think there could be a couple of more cases one would want to have coverage for?
flaeppe: Oh, and I completely missed out on #532 ....
codecov-commenter: # [Codecov](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#647](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (c075d35) into [main](https://codecov.io/gh/django-money/django-money/commit/2040eff83fef2d4a1d882c016ab2b5e0853d2f84?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (2040eff) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/django-money/django-money/pull/647/graphs/tree.svg?width=650&height=150&src=pr&token=S9wbtL0BRG&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

```diff
@@           Coverage Diff           @@
##             main     #647   +/-   ##
=======================================
  Coverage   97.94%   97.94%           
=======================================
  Files          29       29           
  Lines         974      974           
  Branches      165      191   +26     
=======================================
  Hits          954      954           
  Misses         13       13           
  Partials        7        7           
```


| [Impacted Files](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [djmoney/models/fields.py](https://codecov.io/gh/django-money/django-money/pull/647/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-ZGptb25leS9tb2RlbHMvZmllbGRzLnB5) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [2040eff...c075d35](https://codecov.io/gh/django-money/django-money/pull/647?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).

jrocketfingers: > Oh, and I completely missed out on #532 ....

No worries, I'm just glad that someone picked up the torch 🎉 

Patch:
diff --git a/djmoney/models/fields.py b/djmoney/models/fields.py
index 9b7195e..9d42575 100644
--- a/djmoney/models/fields.py
+++ b/djmoney/models/fields.py
@@ -298,8 +298,11 @@ class MoneyField(models.DecimalField):
 
         if self._has_default:
             kwargs["default"] = self.default.amount
-        if self.default_currency is not None and self.default_currency != DEFAULT_CURRENCY:
-            kwargs["default_currency"] = str(self.default_currency)
+        if self.default_currency != DEFAULT_CURRENCY:
+            if self.default_currency is not None:
+                kwargs["default_currency"] = str(self.default_currency)
+            else:
+                kwargs["default_currency"] = None
         if self.currency_choices != CURRENCY_CHOICES:
             kwargs["currency_choices"] = self.currency_choices
         if self.currency_field_name:


Test patch:
diff --git a/tests/test_models.py b/tests/test_models.py
index f573ed3..506fac4 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -787,3 +787,11 @@ def test_mixer_blend():
         instance = mixer.blend(ModelWithTwoMoneyFields)
         assert isinstance(instance.amount1, Money)
         assert isinstance(instance.amount2, Money)
+
+
+def test_deconstruct_includes_default_currency_as_none():
+    instance = ModelWithNullableCurrency()._meta.get_field("money")
+    __, ___, args, kwargs = instance.deconstruct()
+    new = MoneyField(*args, **kwargs)
+    assert new.default_currency == instance.default_currency
+    assert new.default_currency is None