SQLALchemy添加了显着的重载. SQLAlchemy对象没有属性'dateTime [英] SQLALchemy adds significant overload. SQLAlchemy Object has no attribute 'dateTime
本文介绍了SQLALchemy添加了显着的重载. SQLAlchemy对象没有属性'dateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码有问题,尝试使用python在flask中设置SQLAlchemy数据库.代码:
I have issues with my code, trying to setup SQLAlchemy database in flask using python. Code:
感谢您的帮助.
试图重新安装SQLAlchemy.
Tried to reinstall SQLAlchemy.
我使用的是公司笔记本电脑,应该没问题吗?
I'm using a corporate laptop, shouldn't be a problem?
from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime
def __repr__(self):
return '<Task %r>' % self.id
@app.route('/', methods=['POST', 'GET'])
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
(env) C:\Users\rodrigs\Documents\PythonFlaskApp>app.py
C:\Users\rodrigs\Documents\PythonFlaskApp\env\lib\site-packages\flask_sqlalchemy\__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
File "C:\Users\rodrigs\Documents\PythonFlaskApp\app.py", line 9, in <module>
class Todo(db.Model):
File "C:\Users\rodrigs\Documents\PythonFlaskApp\app.py", line 13, in Todo
date_created = db.Column(db.dateTime, default=datetime.utcnow)
AttributeError: 'SQLAlchemy' object has no attribute 'dateTime'
推荐答案
警告:
FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
将"SQLALCHEMY_TRACK_MODIFICATIONS"设置为True/False.通过添加下面的行,最好为False
Set the 'SQLALCHEMY_TRACK_MODIFICATIONS' to True / False. Preferably False by adding the below line
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
实例化Flask应用之后,即紧接在下面的行之后:
Immediately after instantiation of the Flask app, i.e., after the below line:
app = Flask(__name__)
对于错误:
date_created = db.Column(db.dateTime, default=datetime.utcnow)
AttributeError: 'SQLAlchemy' object has no attribute 'dateTime'
出现拼写错误,类型为db.DateTime.
There is a typo error, the type is db.DateTime.
这篇关于SQLALchemy添加了显着的重载. SQLAlchemy对象没有属性'dateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文