我正在使用声明性 SQLAlchemy 类来执行计算。部分计算要求我对不同表提供的所有配置执行计算,该表在两个表之间没有任何外键关系。
这个类比与我的实际应用完全不同,但希望有助于理解我想要发生的事情。
我有一套汽车和一份油漆颜色清单。汽车对象有一个工厂,它提供所有可能颜色的汽车
from sqlalchemy import *
from sqlachemy.orm import *
def PaintACar(car, color):
pass
Base = declarative_base()
class Colors(Base):
__table__ = u'colors'
id = Column('id', Integer)
color= Column('color', Unicode)
class Car(Base):
__table__ = u'car'
id = Column('id', Integer)
model = Column('model', Unicode)
# is this somehow possible?
all_color_objects = collection(...)
# I know this is possible, but would like to know if there's another way
@property
def all_colors(self):
s = Session.object_session(self)
return s.query(A).all()
def CarColorFactory(self):
for color in self.all_color_objects:
yield PaintACar(self, color)
我的问题:是否有可能以某种方式产生 all_color_objects ?无需求助于查找会话并像 all_colors 属性那样手动发出查询?