I have a very simple implementation of Flask, Flask-Restful, Flask-SqlAlchemy, and Flask-Marshmallow:
I am getting the validation error: {'_schema': ['Invalid input type.']} when trying to dump an object I just created.
init.py
app = Flask(__name__)
configure_rds(app)
db = SQLAlchemy(app)
ma = Marshmallow(app)
api = Api(app)
register_blueprints(app)
model.py
class MyModel(db.Model):
__tablename__ = 'my_table'
id = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.String(255))
other_id = db.Column(db.Integer)
last_modified = db.Column(db.Date)
status = db.Column(db.String(255))
Schemas.py
class MyModelSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = MyModel
load_instance = True
id = ma.auto_field()
uid = ma.auto_field()
other_id = ma.auto_field()
last_modified = ma.auto_field()
status = ma.auto_field()
views.py
class MyModelView(Resource):
def get(self):
maps = MyModel.query.all()
ma_schema = MyModelSchema(many=True)
body = ma_schema.dump(maps)
resp = jsonify(body)
resp.status_code = 200
new_map = maps[0]
return resp
def post(self):
args = self.parser.parse_args()
ma_schema = MyModelSchema(many=False)
# new_model = ms.load(args, session=db.session)
# new_model .last_modified = date.today()
new_model = MyModel(uid=args['uid'],
other_id=args['other_id'],
status=args['status'],
last_modified=datetime.date.today())
db.session.add(new_model )
db.session.commit()
body = ma_schema.dump(new_model)
resp = jsonify(body)
resp.status_code = 200
The get method works just fine. The post will add the new model to the database but then fail to serialize the model with the schema. It is also unable to load my reqparse args into the schema as well. Why would it be able to add to the RDS, read from the RDS, and yet not be able to serialize the object?
I have tried a bunch of different column types, tried various setups of the schema, nothing seems to work. Also I have verified that the exact types of the model's attributes in the get match the attributes of the model I am creating and attempting to dump... So I am really unsure of what else it could be.