我正在使用棉花糖架构和sqlalchemy模板创建POST和PUT路由,但是当我执行PUT或POST请求时,出现错误。当我验证接收到的对象与架构相对应时,会生成此错误。
class EquipmentModel(db.Model):
__tablename__ = "equipments"
id = db.Column(db.Integer, db.Sequence('equipment_id_seq'), primary_key=True, autoincrement=True)
model = db.Column(db.String(50), nullable=False)
serial_number = db.Column(db.String(50), nullable=False)
brand = db.Column(db.String(50), nullable=False)
def __init__(self, model, serial_number, brand, *args):
super().__init__(*args)
self.model = model
self.serial_number = serial_number
self.brand = brand
def __repr__(self):
return "<EquipmentModel('%d', '%s')>" % (self.id, self.brand)
from flask_marshmallow import Marshmallow
from marshmallow import fields
ma = Marshmallow()
class EquipmentSchema(ma.ModelSchema):
id = fields.Integer(dump_only=True)
brand = fields.String(required=True)
model = fields.String(required=True)
serial_number = fields.String(required=True)
schema = EquipmentSchema()
body = request.get_json(silent=True, force=True)
body, errors = schema.load(body)
AttributeError: 'NoneType' object has no attribute '__mapper__'
答案 0 :(得分:0)
我认为问题可能是您需要像这样在Meta中为模式提供模型
class EquipmentSchema(ma.ModelSchema):
id = fields.Integer(dump_only=True)
brand = fields.String(required=True)
model = fields.String(required=True)
serial_number = fields.String(required=True)
class Meta:
fields = ("id", "brand", "model", "serial_number")
model = EquipmentModel