I have a custom method inside one of the Django models, it does some routine DB data fetching and summing. Here is it:
from django.utils.timezone import utc, now
...
class Project(models.Model):
...
def getTotalMonthlyBackers(self, monthdate=now()):
from pledger.models import DonationHistory
return (DonationHistory.objects
.filter(project=self)
.filter(datetime_sent__gte=datetime(monthdate.year, monthdate.month, 1))
.aggregate(Count('user', distinct=True))['user__count'])
when I try to call the method through
print project.getTotalMonthlyBackers()
I’m getting error: 'NoneType' object is not callable
However if I output the method itself just to check if it’s not a typo:
print project.getTotalMonthlyBackers
I get: <bound method Project.getTotalMonthlyBackers of <Project: Putty>>
So the method is there and bound to the certain Project object.
There is something very obvious in Python I can’t get about calling this method, but I couldn’t figure it out from the manuals.
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/mnt/hgfs/Projects/BitFund/Sources/source/project/views/project_view_details.py" in view
101. print project.getTotalMonthlyBackers()
File "/mnt/hgfs/Projects/BitFund/Sources/source/project/models.py" in getTotalMonthlyBackers
76. .filter(datetime_sent__gte=datetime(monthdate.year, monthdate.month, 1))
Exception Type: TypeError at /putty_1
Exception Value: 'NoneType' object is not callable
It seems that you haven’t imported the
datetimeconstructor yet, also if you use it you will get a naive datetime, which will produce a warming because that structure doesn’t contain atzinfoobject, so try using a Time Zone aware constructor for that datetime or use themake_aware.Check out this piece of documentation about the topic, and I’m sure you will solve it in the most proper way Timezones and utils