小编Art*_*haz的帖子

“棉花糖”对象没有属性“ModelSchema”

从文档中看一切都很好,但是当我运行应用程序时它仍然给我这个错误:

  File "main.py", line 21, in <module>
    class UserSchema(ma.ModelSchema):
AttributeError: 'Marshmallow' object has no attribute 'ModelSchema'
Run Code Online (Sandbox Code Playgroud)

一切都正确导入。DB 已提交。pipenv 和 venv 的行为是相同的。

我错过了什么吗?

from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy 
from flask_marshmallow import Marshmallow

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///marshmallowjson.db'

db  = SQLAlchemy(app)
ma = Marshmallow(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    item_name = db.Column(db.String(50))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User', backref='items')

class UserSchema(ma.ModelSchema):
    class Meta:
        model = User …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy flask marshmallow

11
推荐指数
3
解决办法
1万
查看次数

标签 统计

flask ×1

marshmallow ×1

python ×1

sqlalchemy ×1