鉴于sqlalchemy
中有几个简单的一对多关系表,我试图编写一个通用函数来将子句添加到关系集合中。表格如下所示:
class StockItem(Base):
__tablename__ = 'stock_items'
stock_id = Column(Integer, primary_key=True)
description = Column(String, nullable=False, unique=True)
department = Column(String)
images = relationship('ImageKey', backref='stock_item', lazy='dynamic')
def __repr__(self):
return '<StockItem(Stock ID:{}, Description: {}, Department: {})>'.\
format(self.stock_id, self.description, self.department)
class ImageKey(Base):
__tablename__ = 'image_keys'
s3_key = Column(String, primary_key=True)
stock_id = Column(Integer, ForeignKey('stock_items.stock_id'))
def __repr__(self):
return '<ImageKey(AWS S3 Key: {}, Stock Item: {})>'.\
format(self.s3_key, self.stock_id)
因此,对于给定的设置,我可以将images
集合中的项目添加到给定的StockItem
:
item = StockItem(stock_id=42, description='Frobnistication for Foozlebars',
department='Books')
image = ImageKey(s3_key='listings/images/Frob1.jpg', stock_id=42)
item.images.append(image)
确定。到现在为止还挺好。实际上,我的应用程序将有几个关系表。当我尝试将其概括为处理任意关系的函数时,我的问题就出现了。这就是我写的内容(注意,add_item()
只是一个用try/except
包装对象构造来处理IntegrityError
的方法:
@session_manager
def _add_collection_item(self, Parent, Child, key, collection,
session=None, **kwargs):
"""Add a Child object to the collection object of Parent."""
child = self.add_item(Child, session=session, **kwargs)
parent = session.query(Parent).get(key)
parent.collection.append(child) # This line obviously throws error.
session.add(parent)
我将这个函数称为:
db._add_collection_item(StockItem, ImageKey, 42, 'images',
s3_key='listings/images/Frob1.jpg',
stock_id=42)
这显然是错误,因为父表没有collection
属性。回溯:
Traceback (most recent call last):
File "C:\Code\development\pyBay\demo1.py", line 25, in <module>
stock_id=1)
File "C:\Code\development\pyBay\pybay\database\client.py", line 67, in add_context_manager
result = func(self, *args, session=session, **kwargs)
File "C:\Code\development\pyBay\pybay\database\client.py", line 113, in _add_collection_item
parent.collection.append(child)
AttributeError: 'StockItem' object has no attribute 'collection'
我认为无论如何都会抛出错误,因为集合名称是作为字符串传递的。
所以我的问题是,如何通过关键词而不是使用'dot'表示法将项目添加到集合中?
答案 0 :(得分:4)
执行:
getattr(parent, collection).append(child)
更多信息