-
Notifications
You must be signed in to change notification settings - Fork 312
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
fixing issue with django rest framework when using MinMoneyValidator/MaxMoneyValidator #722
Conversation
issue : MinMoneyValidator or MaxMoneyValidator with ModelSerializer error : Decimal object has no attribute amount solution : getting default currency from source field on django model expiation : default_currency is not passed to MoneyField Serializer
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.
LGTM! To elaborate: This is fine to me, even without a test. It lives in the .contrib
space so I wouldn't require a test unless @hosamhamdy258 you want to add one? :)
I'd ask for a test that demonstrates the issue, so we can be sure we have all the use cases in our test suite so we don't break it accidentally in the future. |
@hosamhamdy258 would you be able to provide a test case for this? |
Hello @benjaoming , we are also hitting this issue. What do we need in order to get this PR merged? Do you want me to write a test case since it's been a few months? |
sorry for being late i would like to get your help to make this fix and test cases |
go ahead would be so appreciated |
@errietta the test case is most welcome and will make this PR merged and very likely also go into a quick little patch release 👍 🙏 |
@benjaoming Can you help here? I'm trying to run the tests even against
|
@errietta hmm, that could be a missing pytest plugin. Try to run |
I can see that we have some issues in the stack. We need to upgrade pytest to work with the latest pytest-django. And that somehow triggers the above error. |
@benjaoming I'm not sure where to push but here is a test that fails in
You can also do it this way and it's the same issue
.. I wonder if this issue only manifests because the minimum is 0? I got the tests to run with |
The failure:
|
@benjaoming Digging further, the other thing I'm missing here is |
@errietta you should be able to do that with the |
@errietta you can try to merge in the latest |
@benjaoming here is a test that fails on main, but works on this branch
again sorry for just pasting here but I don't have commit permissions neither in the original repo nor the fork |
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #722 +/- ##
==========================================
- Coverage 95.85% 90.60% -5.25%
==========================================
Files 28 28
Lines 964 969 +5
Branches 166 160 -6
==========================================
- Hits 924 878 -46
- Misses 29 78 +49
- Partials 11 13 +2 ☔ View full report in Codecov by Sentry. |
@benjaoming I see you did not add the latest version sorry :'0 |
yes it's your latest test case, I just updated a few things |
@benjaoming the thing is that @hosamhamdy258 's fix works if there's a |
@errietta it seems to work if there's a DEFAULT_CURRENCY defined... but yeah, it's generally important to define currencies in these validations. I don't really mind that it breaks in cases where the currency has become implicit. |
@benjaoming if you want to enforce that behaviour maybe it would be good to have an actual error message rather than just let the code die with a cryptic message.. just IMO |
I'm just trying to release this feature, I don't use django-rest-framework. My assessment is that "yes there might be ways to make it correctly ignore a missing currency value, but this is largely not desirable, so that case can be ignored". |
Caused a regression at #757 |
Fixes django-money#757 from original regression caused by django-money#722
I got some additional slightly weird behavior but tough to tell if it's an actual regression or not. I recall now that this PR is just another version of some research I did a couple of years ago on #665 (comment). I have been running an overridden version of The root is that there are cases where the deserialized values from DRF are sometimes a I had some tests fail on my end and I realize that the problem with this current fix is there is no way to differentiate between currency being explicitly passed in/set as a default on the field and currency being read from the system default. From request JSON data that looks like this: {'category': 'OTHER', 'unit_amount': 10, 'unit_name': 'Safety Fee', 'unit_quantity': 4} where the Django
(by the way - this is another weird thing, this field is being automatically created by DRF but the Old behavior in 3.4: New behavior in 3.5: Anyway, at the end of the day it's probably better to be consistent and always return |
The more I think about it the more I think what has been done here is probably the right thing to do. For my particular case to differentiate between using the system default currency and an explicitly provided currency for the field I can use |
issue : MinMoneyValidator or MaxMoneyValidator with ModelSerializer
error : Decimal object has no attribute amount
solution : getting default currency from source field on django model
expiation : default_currency is not passed to MoneyField Serializer