问题
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:
Change
INSTALLED_APPS
insettings.py
as follows:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.messages', 'django.contrib.staticfiles', 'app' ]
Change
SESSION_ENGINE
in `settings.py as follows:SESSION_ENGINE='app.sessions'
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
In
./app/models/models.py
, I havefrom 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
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