[PYTHON] Suppressed "Field may not be null." Appearing in fields that are not null and default in DB definition with connexion and marshmallow-sqlalchemy

It worked for the time being! But there seems to be a better way.

environment

requirement.txt


#Excerpt
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 in question

model.py


hoge_date = db.Column(db.Date(), nullable=False, default=date.fromisoformat('2099-12-31'))

error contents

console.log


marshmallow.exceptions.ValidationError: {'hoge_date': ['Field may not be null.']}

Coping

schema.py


class HogeSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Hoge

    #Add this
    @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

Recommended Posts

Suppressed "Field may not be null." Appearing in fields that are not null and default in DB definition with connexion and marshmallow-sqlalchemy
Upload and manage packages that are not in conda to anaconda.org