python - 无法将 datetime.datetime 与 builtin_function_or_method 进行比较

标签 python django datetime

<分区>

我是 Django 的新手。

我创建了赞助商模型,该模型具有 start_date(成为赞助商的开始日期)和 end_date(赞助商的结束日期)。

start_date = models.DateField(
        _("Start date"),
        default=datetime.date.today)

end_date = models.DateField(
        _("End date"),
        default=datetime.date.today)

我想将所有逻辑放在模型中,如果不可能,那么我想将逻辑放在 View 中。我制作了可以返回 True 或 False 的方法 current_sponsor(如果今天在 start_date 和 end_date 的范围内意味着 True 否则 False)。

这是我的current_sponsor方法

def current_sponsor(self):
        today = datetime.date.today
        if today >= self.start_date:
            return True
        elif today <= self.end_date:
            return True
        else:
            return False

问题是我遇到了错误无法将 datetime.datetime 与 builtin_function_or_method 进行比较

我尝试使用 django shell 来查看数据,它似乎有效,但实际上不起作用。

最佳答案

datetime.date.today 没有调用您认为的函数:

>>> import datetime
>>> datetime.date.today
<built-in method today of type object at 0x7fb681a90f80>  # NOT CALLING FUNCTION

>>> datetime.date.today()  # You need () at the end
datetime.date(2015, 11, 4) 

如果添加括号,您将获得预期的结果。

关于python - 无法将 datetime.datetime 与 builtin_function_or_method 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33523578/

相关文章:

python - 是否可以对多个图像使用 cv2.HoughCircles 函数?

python - 如何添加脚本任务来在 Bamboo 中运行 pytest

python - Django 注册 Redux : how to change the unique identifier from username to email and use email as login

django - httpd.conf 在哪里?

c# - TextBoxFor 中的 MVC3 格式可为 nullable DateTime,无需更改模型

python - pip 安装 ssh-import-id 时没有名为 'requests' 的模块

django - STATIC_URL不起作用

python - 根据午夜时间更改日期

delphi - DateUtils 中的 IncHour 过程

python - 使用 Pandas AWS Glue Python Shell 作业