为SQLAlchemy添加额外的列类型
项目描述
此仓库包含我用于不同项目的自定义列类型。
代码应与SQLAlchemy 1.2.3 - 1.4.x兼容。
pip install ColumnAlchemy
UTCDateTime
UTCDateTime
将Python tz-aware datetime.datetime
值存储为数据库中的UTC日期时间(没有显式的时间信息)。我使用它来引入在数据库中期望“天真”日期时间的系统中对tz-aware时区。
from schwarz.column_alchemy import UTCDateTime
class Foo(Base)
__tablename__ = 'foo'
id = Column(Integer, autoincrement=True, primary_key=True)
timestamp = Column(UTCDateTime)
ShiftedDecimal
ShiftedDecimal
将Decimal
作为整数存储在数据库中(具有有限的精度)。这对于在需要特殊处理以存储十进制的sqlite中存储十进制值特别有用。
from decimal import Decimal
from schwarz.column_alchemy import ShiftedDecimal
class Foo(Base)
__tablename__ = 'foo'
id = Column(Integer, autoincrement=True, primary_key=True)
percentage = Column(ShiftedDecimal(4))
foo = Foo(percentage=Decimal('1.2324'))
# stores percentage as 12324 in the database but returns the
# correct Decimal value after loading.
ValuesEnum
待办事项
IntValuesEnum
待办事项
YearMonthColumn
YearMonth
类似于datetime.date
,但没有day
属性。它可以用来表示日历月份,并提供一些方便的方法,如first_date_of_month()
和last_date_of_month()
。一个YearMonthColumn
将YearMonth
实例存储为数据库中的“YYYY-MM”。
from schwarz.column_alchemy import YearMonth, YearMonthColumn
class Foo(Base)
__tablename__ = 'foo'
id = Column(Integer, autoincrement=True, primary_key=True)
month = Column(YearMonthColumn())
foo = Foo(month=YearMonth(2020, 7))
# stores "month" as "2020-07" in the database but returns a
# YearMonth instance after loading.
YearMonthIntColumn
与YearMonthColumn
非常相似,但将YearMonth(2020, 7)
存储为整数202007
。
项目详情
关闭
ColumnAlchemy-0.9.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4be35785f38e4400a0e87be525722e2705c50b17f415d1dbcaf81eb5bbea24fc |
|
MD5 | bf5be135a664a807663926f46ad6d1ed |
|
BLAKE2b-256 | ff38938882d6e951e78e6957a30cad17f98bba6154dfb249d2817ea530ae5559 |
关闭
ColumnAlchemy-0.9.2-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 68c97c365c424ada34922a5ef111df5fd2d79fa4615d9e8b21b629de15b8c480 |
|
MD5 | 724662eea7b39d3543f62246da93f1d1 |
|
BLAKE2b-256 | f875a7a217b98c8414fba2ab74751ad60dff35a5b2632edfb7a3fe3e1e051efc |