【发布时间】:2012-05-22 21:13:45
【问题描述】:
使用 SQLAlchemy,可以为每个函数添加默认值。据我了解,这也可能是可调用的(没有任何参数或带有可选的 ExecutionContext 参数)。
现在在声明性场景中,我想知道是否有可能以某种方式使用正在存储的 object 调用的默认函数。 IE。可能是这样的:
Base = sqlalchemy.ext.declarative.declarative_base()
class BaseEntity(Base):
value = Column('value', String(40), default=BaseEntity.gen_default)
def gen_default(self):
# do something with self, for example
# generate a default value using some other data
# attached to the object
return self.default_value
这样的事情可能吗?或者我是否必须为此设置一个 before-insertion 挂钩(如何?)?
【问题讨论】:
标签: python sqlalchemy hook