SQLAlchemy JSON as blob/text

后端 未结 8 1906
猫巷女王i
猫巷女王i 2020-12-02 13:42

I\'m storing JSON down as blob/text in a column using MySQL. Is there a simple way to convert this into a dict using python/SQLAlchemy?

8条回答
  •  抹茶落季
    2020-12-02 14:14

    This is what I came up with based on the two answers above.

    import json
    
    class JsonType(types.TypeDecorator):    
    
        impl = types.Unicode
    
        def process_bind_param(self, value, dialect):
            if value :
                return unicode(json.dumps(value))
            else:
                return {}
    
        def process_result_value(self, value, dialect):
            if value:
                return json.loads(value)
            else:
                return {}
    

提交回复
热议问题