Fix query lookup when using Money object - #13
Merged
Merged
Conversation
VictorTan93
commented
Dec 23, 2025
| ent = MoneyModelDefaults.objects.get(pk=ent.id) | ||
| assert ent.price == Money(100, "USD") | ||
|
|
||
| def test_lookup(self) -> None: |
Author
There was a problem hiding this comment.
Moved to test_django_lookups.py
ewendlick
reviewed
Dec 23, 2025
| class MoneyField(InfiniteDecimalField): | ||
| description = gettext_lazy("An amount and type of currency") | ||
|
|
||
| add_currency_field: bool |
There was a problem hiding this comment.
NAB: Very minor, but what do you think about calling this something a little different, like contains_currency_field, has_currency_field, with_currency_field, or using_currency_field?
"add_currency_field" feels a little ambiguous to me, whether it is for adding a currency field to an existing instance, for positive amounts, or if it is a method
Author
There was a problem hiding this comment.
Ahhh this is not introduced by me, this line was added for mypy typing.
Changing this will affect everyone using it
ewendlick
approved these changes
Dec 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
Fix query lookup when using Money object
This PR fixes an issue with Django ORM query lookups when using
Moneyobjects directly in filters.Previously, querying with a
Moneyobject (e.g.Model.objects.filter(price=Money(100, 'USD'))) would only compare the amount without checking the currency, leading to incorrect results when multiple currencies are present in the database.📝 Changes
Core Fix:
Implemented custom lookup classes (
MoneyExactLookup,MoneyLtLookup,MoneyLteLookup,MoneyGtLookup,MoneyGteLookup):Moneyobjects by checking both amount and currency in the SQL queryMoving away from
MoneyManagerapproach, because it is easily overwritten when a custom manager is used in a Django Model (i.e.objects = CustomManager()).Code Cleanup:
MoneyManagerclassMoneyFieldProxyinto its own module (proxy.py) for better organizationcurrency_field_name,currency_field_db_column) intoutils.pyTesting:
Added comprehensive test suite in
test_django_lookups.pycovering:Behavior
When filtering with a
Moneyobject:The generated SQL now checks both the amount column AND the currency column:
This ensures that only records with matching currency are returned, preventing incorrect comparisons between amounts in different currencies.
✅ Pre-review checklist for the PR author