我在其中一个 Django 模型中有一个自定义方法,它执行一些常规的数据库数据获取和求和。就这个:
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'])
当我尝试通过调用该方法时
print project.getTotalMonthlyBackers()
我收到错误:'NoneType' object is not callable
但是,如果我输出方法本身只是为了检查它是否不是错字:
print project.getTotalMonthlyBackers
我得到:<bound method Project.getTotalMonthlyBackers of <Project: Putty>>
所以该方法在那里并且绑定到某个Project
对象。Python中有一些非常明显的东西我无法调用这个方法,但我无法从手册中弄清楚。
追溯:
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