简体   繁体   中英

How do I access a method that's inside a python inner class?

I am using flask-sessions extension to store user sessions in the database and now trying to query this table to get stored sessions. The session table is created the following way,

SqlAlchemySessionInterface(app, db, table='Session', key_prefix='')

now I'd like to get all the entries using the model query call like this,

Session.query.all()

But I am not able to do that due to the fact that the Session class in a inner class of the SqlAlchemySessionInterface class. Any idea how I can go about querying the table?

The complete SqlAlchemySessionInterface class code below,

class SqlAlchemySessionInterface(SessionInterface):

    serializer = pickle
    session_class = SqlAlchemySession

    def __init__(self, app, db, table, key_prefix, use_signer=False,
                 permanent=True):
        if db is None:
            from flask_sqlalchemy import SQLAlchemy
            db = SQLAlchemy(app)
        self.db = db
        self.key_prefix = key_prefix
        self.use_signer = use_signer
        self.permanent = permanent

        class Session(self.db.Model):
            __tablename__ = table

            id = self.db.Column(self.db.Integer, primary_key=True)
            session_id = self.db.Column(self.db.String(255), unique=True)
            data = self.db.Column(self.db.LargeBinary)
            expiry = self.db.Column(self.db.DateTime)

            def __init__(self, session_id, data, expiry):
                self.session_id = session_id
                self.data = data
                self.expiry = expiry

            def __repr__(self):
                return '<Session data %s>' % self.data

        # self.db.create_all()
        self.sql_session_model = Session

In your code, should have from flask_session import Session and Session(app) .

Bind the return object of Session(app) to an variable, such as sess .

Then, session_model = sess._get_interface(app).sql_session_model .

Then, you can session_model.query.all() .

Key point : look at the source code of Session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM