Extending database-backed session engines

此生再无相见时 提交于 2021-02-07 10:42:07

问题


I plan to extend the default sessionstore by adding an account_id, for this I followed this link extending-database-backed-session-engines, but the account_id is not being created on migrate.

The application is located in app folder.

In ./settings.py. I have:

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

According to this link Extending the Session Middleware I have:

SESSION_ENGINE='app.sessions'

In ./sessions.py. I have:

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

Edit: With this configuration the sessions table is not created.

I also tried to to add the sessions.py file in a sessions module, and to add app.sessions to the installed_apps section in settings, the same result.

Do I also need to modify the migrations file to create the Session table ?

SOLUTION

I've solved the issue. The solution is:

  1. Change INSTALLED_APPS in settings.py as follows:

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'app'
    ]
    
  2. Change SESSION_ENGINE in `settings.py as follows:

    SESSION_ENGINE='app.sessions'
    
  3. In ./app/sessions.py, I have:

    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. In ./app/models/models.py, I have

    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. Regular makemigrations and migrate. And all should be running.


回答1:


If you have an app with a custom session model, you should include this in your INSTALLED_APPS instead of django.contrib.sessions.

Having

SESSION_ENGINE='app.sessions'

looks wrong to me. SESSION_ENGINE should be the path to your AbstractBaseSession subclass.



来源:https://stackoverflow.com/questions/34678211/extending-database-backed-session-engines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!