使用下面的代码,我打算使用datetime.utcnow()值作为该字段的默认值,但它提供了一个属性错误消息,其中显示了' SQLALchemy'对象没有属性'日期时间'
from flask import Flask, flash, render_template, redirect, request, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///HRS.sqlite3'
app.config['SECRET_KEY'] = 'any key'
db = SQLAlchemy(app)
class Treatment(db.Model):
treatmentDate = db.Column('Date of treatment', db.Datetime, default=datetime.utcnow())
答案 0 :(得分:2)
在代码中,评论已经说了什么:
# rest stays the same
class Treatment(db.Model):
treatmentDate = db.Column('Date of treatment', db.DateTime, default=datetime.utcnow)
“传递函数而不调用它”意味着将括号()留在末尾。像你一样调用函数实际上会传入一个datetime对象,但是你想要的是将函数本身作为默认值传递给你在创建一个新的Treatment实例时调用。