For the following Model, I added a new field gst_amount
:
from djmoney.models.fields import MoneyField
class Invoice(Model):
gst_amount = MoneyField(max_digits=14, decimal_places=2, default="0 USD")
The migration to add this field works fine and I can use the field in my views without issue.
When I try to run the following data migration:
from djmoney.money import Money
from django.db import migrations
def migrate_invoices(app, _):
Invoice = app.get_model("users", "Invoice")
for invoice in Invoice.objects.all():
invoice.gst_amount = Money(10, 'AUD')
class Migration(migrations.Migration):
operations = [
migrations.RunPython(migrate_invoices, migrations.RunPython.noop)
]
I get the following error:
File "/app/blah/users/migrations/0159_auto_20230929_0113.py", line 9, in migrate_invoices
for invoice in Invoice.objects.all():
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 280, in __iter__
self._fetch_all()
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 69, in __iter__
obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 515, in from_db
new = cls(*values)
File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 437, in __init__
_setattr(self, field.attname, val)
File "/usr/local/lib/python3.9/site-packages/djmoney/models/fields.py", line 111, in __set__
and self.field._currency_field.null
AttributeError: 'MoneyField' object has no attribute '_currency_field'
Not sure what I'm missing?
This appears to an issued introduced in the current version (3.3). As a workaround, downgrading to v3.2 avoids this issue.