New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should be possible to change and save money back to default currency #227
Conversation
The following test breaks when changing the behavior:
|
The explanation is already in an older comment:
|
Any advice? Ideas? |
@benjaoming |
It seems like our builds are broken, I'll fix it tomorrow and rerun all PRs |
6662467
to
b5f4dff
Compare
For me it seems perfectly fine! Probably we could just set currency like this: def prepare_value(self, obj, value):
validate_money_value(value)
currency = get_currency(value)
if currency:
setattr(obj, self.currency_field_name, currency)
return self.field.to_python(value)
Good: {'money': Money(0, 'EUR')} Also good: {'money': 10, 'money_currency': 'PLN'} Bad:
Now we manipulate with currency if currency is absent - we're setting default value for it. from .fields import MoneyField, CurrencyField
...
def _expand_money_kwargs(model, args=(), kwargs=None, exclusions=()):
"""
Augments kwargs so that they contain _currency lookups.
"""
involved_fields = [_get_clean_name(name) for name in kwargs]
for name, value in list(kwargs.items()):
if name in exclusions:
continue
if isinstance(value, Money):
clean_name = _get_clean_name(name)
kwargs[name] = value.amount
kwargs[get_currency_field_name(clean_name)] = smart_unicode(value.currency)
else:
field = _get_field(model, name)
if isinstance(field, MoneyField):
if isinstance(value, (BaseExpression, F)):
clean_name = _get_clean_name(name)
if not isinstance(value, F):
value = prepare_expression(value)
kwargs[get_currency_field_name(clean_name)] = F(get_currency_field_name(value.name))
if is_in_lookup(name, value):
args += (_convert_in_lookup(model, name, value), )
del kwargs[name]
elif isinstance(field, CurrencyField):
money_field_name = name[:-9] # Remove '_currency'
if money_field_name not in involved_fields:
money_field = _get_field(model, money_field_name)
kwargs[money_field_name] = money_field.default.amount
return args, kwargs What do you think? All tests pass with these changes. I tried Django 1.4, 1.10. |
@Stranger6667 thanks, seems to make sense -- so with the changes, it makes it possible to do all this?
...or was this functional even before the change? |
It was functional :) |
Hmm, not sure :) would you like to write a sentence for the changelog? :) |
Current coverage is 90.48% (diff: 100%)@@ master #227 diff @@
==========================================
Files 16 16
Lines 719 725 +6
Methods 0 0
Messages 0 0
Branches 136 139 +3
==========================================
+ Hits 650 656 +6
Misses 48 48
Partials 21 21
|
Don't know, it should be drop in replacement :) Also, I'll check more queries, which could be wrong |
Ah, I thought, that you mean some extra line about |
Oh snap, I thought I had already written release notes for the fix... I did that only in the other PR yesterday.. I'll add something here... |
817351e
to
5aaae14
Compare
Rebased and added release note |
@@ -219,4 +220,7 @@ Changes in 0.3 | |||
.. _tsouvarev: https://github.com/tsouvarev | |||
.. _w00kie: https://github.com/w00kie | |||
.. _willhcr: https://github.com/willhcr | |||
<<<<<<< e6efad2071e1281609732cc5c9d5c1cbf5c17c18 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its probably should not be there :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No indeed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I just force-pushed the fix, wonder who made it first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You did ;)
#221