从SQLAlchemy查询中检索不同的backref实例

时间:2013-05-21 23:40:16

标签: python orm sqlalchemy

鉴于以下模型:

class Foo(db.model):
    id = db.Column(db.Integer, primary_key=True)
    bar_id = db.Column(
        "bar_id",
        db.Integer(),
        db.ForeignKey("bar.id"))
    baz_id = db.Column(
        "baz_id",
        db.Integer(),
        db.ForeignKey("baz.id"))
    bars = db.Relationship(
        "Bar",
        backref=backref("bar_foo", lazy="dynamic"))
    bazs = db.relationship(
        "Baz",
        backref=backref("baz_foo", lazy="dynamic"))

class Bar(db.model):
    id = db.Column(db.Integer, primary_key=True)
    # some more columns

class Baz(db.model):
    id = db.Column(db.Integer, primary_key=True)
    # some more columns

q = Bar.query.get(123).bar_foo
# .all() gives me several Foo instances

我想为q结果检索不同的Baz实例

1 个答案:

答案 0 :(得分:0)

获取查询返回的一组结果。默认情况下,object返回的是 hashable ,因此set函数可以将两个Foo object区分开,并确保您在{{1}中获得的内容实际上是截然不同的。