从子级到父级的flask-sqlalchemy反向模型关系查询

时间:2018-08-21 13:00:20

标签: python sqlalchemy flask-sqlalchemy

我已经定义了这些一对一模型,我正在尝试在parent.method1内调用child.call_parent_method

class Parent(db.Model):
    __tablename__ = 'parent'

    id = db.Column(db.Integer, primary_key=True)
    child = db.relationship('Child', backref='parent')
    child_id = db.Column(db.Integer, db.ForeignKey('child.id'),
                                 nullable=True)

    @hybrid_property
    def prop1(self)
        print 'This is method 1'


class Child(db.Model):
     __tablename__ = 'child'

    id = db.Column(db.Integer, primary_key=True)

    def call_parent_method(self):
        self.parent.prop1

但是我得到的是:

AttributeError: 'InstrumentedList' object has no attribute 'prop1'

0 个答案:

没有答案