我在sqlalchemy中有一个继承方案(这是一个示例代码,我猜逻辑很清楚):
class Base:
@classmethod
def generate(cls, somedata):
base = Base(...)
# populating Base class fields here
...
session.add(base)
session.flush()
return base
class Child(Base):
@declared_attr
def area(self):
return Base.__table__.c.get('field', Column(Float))
@classmethod
def generate(cls):
base = super(Child, cls).generate(cls, somedata)
# not sure if I need this or not. It doesn't matter it seems
# base.__class__ = cls
base.area = 10.5
在这段代码之后,我有一个包含所有需要列的表,并且填充了基类字段。子类字段('区域')为空。 有办法解决吗?