とりあえず動いたからよし!したけどもっと筋の良い方法もありそう。
環境
requirement.txt
# 抜粋
connexion==2.7.0
marshmallow-sqlalchemy==0.23.1
marshmallow==3.7.1
sqlalchemy==1.3.18
flask-marshmallow==0.13.0
flask-migrate==2.5.3
flask-sqlalchemy==2.4.4
flask==1.1.2
問題のColumn
model.py
hoge_date = db.Column(db.Date(), nullable=False, default=date.fromisoformat('2099-12-31'))
エラー内容
console.log
marshmallow.exceptions.ValidationError: {'hoge_date': ['Field may not be null.']}
対処
schema.py
class HogeSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Hoge
# これを追加
@pre_load
def remove_has_defalt_key_if_none(self, in_data: dict, **kwargs):
if in_data.get('hoge_date') is None:
in_data.pop('hoge_date', None)
return in_data