0

假设我有以下内容:

association_table = Table('things_stuffs', Base.metadata,
    autoload=True,
    extend_existing=True)

class Thing(Base):
    __tablename__ = 'things'
    __table_args__ = {'autoload': True}

class Stuff(Base):
    __tablename__ = 'stuffs'
    __table_args__ = (
        {'autoload': True}
        )
    things = relationship(Thing,
                secondary=association_table,
                backref=backref("stuffs", uselist=False, lazy='dynamic'))

现在,如果我想从 Stuff 实例中获取所有内容,我可以这样做:

a_stuff_instance.things.filter().all()

并因lazy="dynamic"零件而查询。但另一边不起作用。如果我想做

a_thing_instance.stuffs.filter().all()

我明白了AttributeError: 'InstrumentedList' object has no attribute 'filter'。我该怎么办?

4

1 回答 1

3

只需添加dynamic到另一边:

things = relationship(Thing,
            secondary=association_table,
            lazy='dynamic', # *** @note: new parameter ***
            backref=backref("stuffs", uselist=False, lazy='dynamic'))
于 2012-12-07T10:16:06.560 回答