1
我有,可以是整数,浮点数或字符串的值,并且我创建不同的列:一个字段与不同的数据类型[SQLAlchemy的]
#declarative
class MyClass(Base):
#id and other Columns
_value_str = Column(String(100))
_value_int = Column(Integer)
_value_float = Column(Float)
def __init__(self,...,value):
self._value_str = value if isinstance(value,(str,unicode)) else None
self._value_int = value if isinstance(value,int) else None
self._value_float = value if isinstance(value,float) else None
和我想这样做:
>>> value = 'text'
>>> my_class = MyClass(value, ...)
>>> my_class.value
>>> 'text'
>>> session.add(my_class)
>>> session.commit()
#specialy this
>>> result = session.query(Myclass).filter(Myclass.value == 'text').one()
>>> print result.value
>>> 'text'
也许我有一个设计问题,我接受任何好主意
我newbe在SQLAlchemy的
谢谢
篦!,我一直在寻找对于类似的东西,但文件是真的韧皮,谢谢 – sacabuche 2010-07-07 09:26:39
看起来,可能会感兴趣,这是一个选项使用MapperExtention http://stackoverflow.com/questions/3020394/sqlalchemy-how-to-map-against-a - 读取 - 只或 - 计算─ property – sacabuche 2010-07-07 09:32:15
注意这个答案,你不能使用db函数,例如sum()在你存储的任何浮点数或整数。 SQLAlchemy的文档实际上相当不错 - 它们必须涵盖大量的基础 - 但是,可以在少数几个真实世界的例子中使用。 – EoghanM 2010-07-07 09:49:24