扩展数据库支持的会话引擎

时间:2016-01-08 13:26:48

标签: python django session

我计划通过添加account_id来扩展默认会话库,为此,我按照此链接extending-database-backed-session-engines,但在迁移时未创建account_id。

该应用程序位于app文件夹中。

./ settings.py 中。我有:

    INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
)

根据此链接Extending the Session Middleware我有:

SESSION_ENGINE='app.sessions'

./ sessions.py 中。我有:

from django.contrib.sessions.backends.db import SessionStore as DBStore
from django.contrib.sessions.base_session import AbstractBaseSession

from django.db import models


class SessionStore(DBStore):
    @classmethod
    def get_model_class(cls):
        return CustomSession

    def create_model_instance(self, data):
        obj = super(SessionStore, self).create_model_instance(data)
        try:
            account_id = int(data.get('_auth_user_id'))
        except (ValueError, TypeError):
            account_id = None
        obj.account_id = account_id
        return obj


class CustomSession(AbstractBaseSession):
    account_id = models.IntegerField(null=True, db_index=True)

    class Meta:
        app_label = 'sessions'

    @classmethod
    def get_session_store_class(cls):
        return SessionStore

编辑:使用此配置,不会创建会话表。

我还尝试在sessions.py模块中添加sessions文件,并将app.sessions添加到installed_apps中的settings部分,同样结果

我是否还需要修改迁移文件以创建会话表?

我已经解决了这个问题。解决方案是:

  1. 更改INSTALLED_APPS中的settings.py,如下所示:

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'app'
    ]
    
  2. 更改`settings.py中的SESSION_ENGINE,如下所示:

    SESSION_ENGINE='app.sessions'
    
  3. ./app/sessions.py中,我有:

    from django.contrib.sessions.backends.db import SessionStore as DBStore
    import app
    
    class SessionStore(DBStore):
        @classmethod
        def get_model_class(cls):
            return app.models.models.CustomSession
    
        def create_model_instance(self, data):
            obj = super(SessionStore, self).create_model_instance(data)
            try:
                user_id = int(data.get('_auth_user_id'))
            except (ValueError, TypeError):
                user_id = None
            obj.user_id = user_id
            return obj
    
  4. ./app/models/models.py,我有

    from django.contrib.auth.models import User
    from django.contrib.sessions.base_session import AbstractBaseSession
    from django.db import models
    
    from app.mysessions import SessionStore
    
    
    class CustomSession(AbstractBaseSession):
        user_id = models.IntegerField(null=True, db_index=True)
    
        class Meta:
            app_label = 'app'
    
        @classmethod
        def get_session_store_class(cls):
            return SessionStore
    
  5. 定期makemigrations和迁移。所有人都应该跑步。

1 个答案:

答案 0 :(得分:2)

如果您的应用具有自定义会话模型,则应将其包含在INSTALLED_APPS而不是django.contrib.sessions中。

具有

SESSION_ENGINE='app.sessions'

对我来说不对。 SESSION_ENGINE应该是AbstractBaseSession子类的路径。