Python peewee字段默认类型和DateTimeField
我已经创建了模型类继承表单Python peewee字段默认类型和DateTimeField,python,peewee,Python,Peewee,我已经创建了模型类继承表单peewee.model import peewee class Example(peewee.Model): id = peewee.IntField(primary_key=True) text = peewee.charField(default="waiting") dt = peewee.DateTimeField(default=datetime.datetime.now().strftime('%Y-%m-%d')) 但是当我在
peewee.model
import peewee
class Example(peewee.Model):
id = peewee.IntField(primary_key=True)
text = peewee.charField(default="waiting")
dt = peewee.DateTimeField(default=datetime.datetime.now().strftime('%Y-%m-%d'))
但是当我在
示例
表中仅为id字段插入新值时,我没有得到默认的文本
值为“waiting”,添加的日期也显示为当前日期时间的0000-00-00 00:00:00
isntead。字段需要是类的成员:
class Example(peewee.Model):
id = peewee.IntField(primary_key=True)
text = peewee.charField(default="waiting")
dt = peewee.DateTimeField(default=datetime.datetime.now)
此外,您希望默认值是datetime的可调用值…否则它将在加载模块时计算datetime.datetime.now(),并且永远不会重新计算它。很抱歉,这只是一个输入错误,因为我从phone手动键入了代码,字段是类的一部分。这解决了问题吗?简而言之,您应该将python datetime对象传递到
dt
字段。