Skip to content

Commit

Permalink
fix for aggregation through proxy model
Browse files Browse the repository at this point in the history
  • Loading branch information
tned73 authored and Stranger6667 committed Nov 29, 2020
1 parent 7fde1fc commit 76004f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions djmoney/models/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ def _get_model(args, func):
if hasattr(func, "__self__"):
# Bound method
model = func.__self__.model
elif hasattr(func, "__wrapped__"):
# Proxy model
model = func.__wrapped__.__self__.model
else:
# Custom method on user-defined model manager.
model = args[0].model
Expand Down
12 changes: 12 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
ModelWithTwoMoneyFields,
ModelWithUniqueIdAndCurrency,
ModelWithVanillaMoneyField,
NotNullMoneyFieldModel,
NullMoneyFieldModel,
ProxyModel,
ProxyModelWrapper,
SimpleModel,
)

Expand Down Expand Up @@ -729,3 +731,13 @@ def extract_data(instance):

qs = ModelWithVanillaMoneyField.objects.order_by("integer").filter(money=Money(10, "AUD"))
assert list(map(extract_data, qs)) == [(Money(10, "AUD"), 1), (Money(10, "AUD"), 2)]


def test_distinct_through_wrapper():
NotNullMoneyFieldModel.objects.create(money=10, money_currency="USD")
NotNullMoneyFieldModel.objects.create(money=100, money_currency="USD")
NotNullMoneyFieldModel.objects.create(money=10, money_currency="EUR")

queryset = ProxyModelWrapper.objects.distinct()

assert queryset.count() == 3
15 changes: 15 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ class NullMoneyFieldModel(models.Model):
field = MoneyField(max_digits=10, decimal_places=2, null=True, default_currency="USD", blank=True)


class ModelManager(models.Manager):
pass


class NotNullMoneyFieldModel(models.Model):
money = MoneyField(max_digits=10, decimal_places=2)

objects = money_manager(ModelManager())


class ProxyModelWrapper(NotNullMoneyFieldModel):
class Meta:
proxy = True


class ProxyModel(SimpleModel):
class Meta:
proxy = True
Expand Down

0 comments on commit 76004f8

Please sign in to comment.