问题
How do I access id
from my child class?
class BaseClass:
id = 'testing'
class MyClass(BaseClass):
something = id
More specifically, I can't access id
from 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:
Here is a little program to test inheritance in your sample example:
class BaseClass:
id = 'testing'
class MyClass(BaseClass):
something = BaseClass.id
if __name__ == '__main__':
print(MyClass().something) # >> "testing"
You should be able to replace id
with BaseModel.id
. If not, please report what error you're getting.
来源:https://stackoverflow.com/questions/52411968/accessing-base-class-variable