扩展数据库支持的会话引擎 [英] Extending database-backed session engines
问题描述
我计划通过添加一个account_id扩展默认的会话存储,为此,我点击了此链接
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.
该应用程序位于 app
文件夹中.
The application is located in app
folder.
在 ./settings.py 中.我有:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.staticfiles',
'app'
)
根据此链接扩展会话中间件我有:
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
编辑:使用此配置,不会创建会话表.
With this configuration the sessions table is not created.
我还尝试将 sessions.py
文件添加到 sessions
模块中,并将 app.sessions
添加到 settings
中的> installed_apps 部分,结果相同.
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.
我还需要修改迁移文件来创建Session表吗?
Do I also need to modify the migrations file to create the Session table ?
我已经解决了这个问题.解决方案是:
I've solved the issue. The solution is:
-
在
settings.py
中更改INSTALLED_APPS
如下:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.staticfiles',
'app'
]
按如下所示在settings.py中更改 SESSION_ENGINE
:
SESSION_ENGINE='app.sessions'
在 ./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
在 ./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
常规makemigrations和迁移.一切都应该在运行.
Regular makemigrations and migrate. And all should be running.
推荐答案
如果您的应用程序具有自定义会话模型,则应将其包含在 INSTALLED_APPS
中,而不要包含在 django.contrib中.sessions
.
If you have an app with a custom session model, you should include this in your INSTALLED_APPS
instead of django.contrib.sessions
.
拥有
SESSION_ENGINE='app.sessions'
在我看来错了. SESSION_ENGINE
应该是您的 AbstractBaseSession
子类的路径.
looks wrong to me. SESSION_ENGINE
should be the path to your AbstractBaseSession
subclass.
这篇关于扩展数据库支持的会话引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!