Best way to force values to uppercase in sqlalchemy field

前端 未结 2 1104
自闭症患者
自闭症患者 2021-01-04 17:22

I am pretty new to python and sqlalchemy and would like to ask for an advice.

What would be the best way to force uppercase values in a SqlAlchemy field when the rec

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 18:01

    Have a look at events

    YOu can easily add a listener for a save/update and just uppercase the string:

    def uppercase_code(target, value, oldvalue, initiator):
        return value.upper()
    
    # setup listener on Item.code attribute, instructing
    # it to use the return value
    listen(Item.code, 'set', uppercase_code, retval=True)
    

提交回复
热议问题