使用棉花糖验证架构时发生AttributeError

时间:2019-07-18 03:07:23

标签: python flask flask-sqlalchemy

我正在使用棉花糖架构和sqlalchemy模板创建POST和PUT路由,但是当我执行PUT或POST请求时,出现错误。当我验证接收到的对象与架构相对应时,会生成此错误。

  • SQLAlchemy模型
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__'

image 1

image 2

image 3

1 个答案:

答案 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