Skip to content

Commit

Permalink
Merge pull request #7 from James-Stokes/bugfix/support-totalling-func…
Browse files Browse the repository at this point in the history
…tions

Fix TypeError when calculating totals from functions in models
  • Loading branch information
rafleze authored Apr 16, 2021
2 parents 273f4c2 + 61f2a0c commit 6a41cf7
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion totalsum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ def changelist_view(self, request, extra_context=None):
if hasattr(self.model, elem):
total = 0
for f in filtered_query_set:
total += getattr(f, elem, 0)
try:
total += getattr(f, elem, 0)
except TypeError:
# This allows calculating totals of columns
# that are generated from functions in the model
# by simply calling the function reference that
# getattr returns
total += getattr(f, elem, 0)()
extra_context["totals"][
label_for_field(elem, self.model, self)
] = round(total, self.totalsum_decimal_places)
Expand Down

0 comments on commit 6a41cf7

Please sign in to comment.