Skip to content

Commit

Permalink
Unconditionally set the currency field when it changes #221
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed Sep 19, 2016
1 parent 40ca15c commit 6662467
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions djmoney/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
from django.db.models.signals import class_prepared
from django.utils import translation

from djmoney import forms
from moneyed import Currency, Money
from moneyed.localization import _FORMATTER, format_money

from djmoney import forms

from .._compat import (
BaseExpression,
Expression,
Expand Down Expand Up @@ -259,9 +258,13 @@ def set_currency(self, obj, value):
# then the currency is already set up, before this code hits
# __set__ of MoneyField. This is because the currency field
# has less creation counter than money field.
#
# Gotcha:
# But we should also allow setting a field back to its original default
# value!
# https://github.com/django-money/django-money/issues/221
object_currency = obj.__dict__[self.currency_field_name]
default_currency = str(self.field.default_currency)
if object_currency != value and (object_currency == default_currency or value != default_currency):
if object_currency != value:
# in other words, update the currency only if it wasn't
# changed before.
setattr(obj, self.currency_field_name, value)
Expand Down

0 comments on commit 6662467

Please sign in to comment.