on the object under the db key of connections dictionary call the method ops.value_to_db_datetime with argument exp, substitute the result for the exp.
exp = connections [ db ] . ops . value_to_db_datetime ( exp )