【发布时间】:2018-01-12 05:43:04
【问题描述】:
这是我的 Flask 应用程序的完整代码:
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/test'
# Order matters: Initialize SQLAlchemy before Marshmallow
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
price = db.Column(db.Float)
class ProductSchema(ma.ModelSchema):
class Meta:
model = Product
@app.route('/')
def hello_world():
products = Product.all()
products_schema = ProductSchema(many=true)
output = products_schema.dump(products).data
return jsonify(output)
如您所见,我尝试从Product 模型中提取数据:
products = Product.all()
我收到了这个错误:
request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\Projects\Dist\backend\index.py", line 27, in hello_world
products = Product.all()
AttributeError: type object 'Product' has no attribute 'all'
【问题讨论】:
-
我是
Product.query.all()。
标签: python flask flask-sqlalchemy flask-restful