Skip to content Skip to sidebar Skip to footer

How Can Sqlalchemy Work With Custom Class As The Attributes

I am using sqlalchemy and flask-sqlalchemy, one of my db model comes with a state column class Item(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column

Solution 1:

You can use a TypeDecorator. Something like:

classStateType(TypeDecorator):
    impl = Integer

    defprocess_bind_param(self, value, dialect):
        return map_integer_to_my_state(value)

    defprocess_result_value(self, value, dialect):
        return map_my_state_to_integer(value)

Then you can use StateType as the type of the state column:

state = db.Column(StateType, ...)

Post a Comment for "How Can Sqlalchemy Work With Custom Class As The Attributes"