def kw_to_sqlalchemy(sqlalchemy_table_cls, kw):
sqlalchemy_table = sqlalchemy_table_cls()
for column, value in kw.iteritems():
try:
column_type = _find_type(sqlalchemy_table_cls, column)
python_type = column_type.python_type
except NameError:
continue
try:
if not issubclass(type(value), python_type):
if issubclass(python_type, bool):
column_value = True if value.lower() == 'true' else False
else:
column_value = python_type(value)
else:
column_value = value
except UnicodeEncodeError:
column_value = unicode(value)
finally:
if issubclass(python_type, datetime):
if column_type.timezone:
# Convert to local time
tz = get_localzone()
column_value = tz.localize(column_value, is_dst=None)
setattr(sqlalchemy_table, column, column_value)
return sqlalchemy_table
评论列表
文章目录