使用:
嗨,我是一个python初学者,来自PHP背景,所以我很抱歉这是一个愚蠢的问题.我试图调用p.was_published_today()时遇到困难.它输出此错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/path/to/mysite/polls/models.py", line 12, in was_published_today
pub_date = models.DateTimeField('date published')
NameError: global name 'datetime' is not defined
Run Code Online (Sandbox Code Playgroud)
但是我的models.py中的代码看起来(对我来说)与我应该根据教程完全一样:
from django.db import models
import datetime
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
# other code but not relevant to the error …Run Code Online (Sandbox Code Playgroud) 在MySQL中,我可以创建一个带有时间字段的表,该值可以高达838:59:59(839小时-1秒).我刚看到在PostgreSQL中,小时字段不能超过23:00:00(24小时).有没有解决的办法?我正在尝试创建一个简单的数据库来跟踪花费多少小时和几分钟的时间,所以它需要超过23小时和几分钟.我可以在MySQL中这样做,但我需要使用PostgreSQL.我用Google搜索,但没找到我要找的东西,所以我希望我没有使用正确的关键字.