So, is there a way for the session, or python, to automatically convert the class to its int representation when save into the db, and convert from the int to its state class when it is retrieved from the db?推荐答案您可以使用 TypeDecorator .像这样:You can use a TypeDecorator. Something like:class StateType(TypeDecorator): impl = Integer def process_bind_param(self, value, dialect): return map_integer_to_my_state(value) def process_result_value(self, value, dialect): return map_my_state_to_integer(value)然后,您可以将StateType用作state列的类型:Then you can use StateType as the type of the state column:state = db.Column(StateType, ...) 这篇关于sqlalchemy如何使用自定义类作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 12:00