数据表模型和循环引用
1、使用models.py保存数据表模型
from exts import db
import datetime
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text)
time = db.Column(db.DateTime, default=datetime.datetime.now)
2、使用exts.py解决循环引用
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
3、app.py(需要在视图函数中操作数据库)
from flask import Flask
from exts import db
from models import Article
app = Flask(__name__)
app.config.from_pyfile('config.py')
db.init_app(app)
@app.route('/')
def index():
db.drop_all()
db.create_all()
return 'Hello World!'
if __name__ == '__main__':
app.run()