访问基类变量

时间:2018-09-19 18:16:43

标签: python sqlalchemy flask-sqlalchemy

如何从子班级访问id

class BaseClass:
    id = 'testing'

class MyClass(BaseClass):
    something = id

更具体地说,我无法从id访问class:User

class BaseModel:
    id = db.Column(db.Integer, primary_key=True)

class User(db.Model, BaseModel):
    username = db.Column(db.String(35), nullable=False, unique=True)

    followed = db.relationship(
        'User',
        secondary=followers,
        primaryjoin=(followers.c.follower_id == id),
        secondaryjoin=(followers.c.followed_id == id),
        backref=db.backref('followers', lazy='dynamic'),
        lazy='dynamic')

1 个答案:

答案 0 :(得分:0)

在您的示例示例中,有一个用于测试继承的小程序:

class BaseClass:
    id = 'testing'

class MyClass(BaseClass):
    something = BaseClass.id

if __name__ == '__main__':
    print(MyClass().something) # >> "testing"

您应该可以将id替换为BaseModel.id。如果没有,请报告您遇到的错误。