我正在尝试将多对多关系的一对多部分转换为 python set()。代码见以下播放示例:
message_tos = Table('message_tos', Base.metadata,
Column('message_id', Integer, ForeignKey('message.id')),
Column('address_id', Integer, ForeignKey('address.id'))
)
class Address(Base):
address = Column(String, nullable=False, unique=True)
emails = relationship('Message')
def __init__(self, address):
self.address = address
class Email(Base):
from = relationship('Address', backref='from')
to = relationship('Address', secondary=message_tos, backref='messages_to')
message_id = Column(String, nullable=False, unique=True)
...
def whatever(*args, **kwargs):
"""
...
"""
email = session.query(Email).filter(Email.message_id==message_id).first()
blah = set(email.to).union(email.from) # This lines throws an error that Address is not iterable
有什么方法可以运行set(email.to)
代码(将地址对象放入集合中),还是我完全错误的方式?显然,我可以只做 set([email.to]),但这是一个额外的复杂顺序(并且这个函数可能会被多次调用,可能很长的 .to 或 .from 列表),我宁愿不这样做有