为django项目创建自定义SessionStore

时间:2016-06-15 07:34:25

标签: python django

如果我想继承django.contrib.sessions.backends.db.SessionStore之类的模块,以便我可以覆盖它的许多默认行为,那么在Django中执行此操作的标准方法是什么?

假设我创建了一个名为mydb.py的文件:

from django.contrib.sessions.backends.db import SessionStore as DBSessionStore

class SessionStore(DBSessionStore):
    ...

如果我的项目在下面有这种结构,最好将mydb.py放在项目文件夹下的backends目录中吗?

myproject
myproject/manage.py
myproject/myproject
myproject/myproject/wsgi.py
myproject/myproject/__init__.py
myproject/myproject/settings.py
myproject/myproject/urls.py
myproject/db.sqlite3
myproject/myapp
myproject/myapp/tests.py
myproject/myapp/admin.py
myproject/myapp/__init__.py
myproject/myapp/models.py
myproject/myapp/apps.py
myproject/myapp/migrations
myproject/myapp/migrations/__init__.py
myproject/myapp/views.py
myproject/myapp/urls.py
myproject/backends
myproject/backends/__init__.py
myproject/backends/mydb.py
myproject/__init__.py

settings.SESSION_ENGINE='backends.db'是否是避免命名空间冲突的合理标准? djamgo配置的一般规则是当前项目是否包含在python搜索路径中?

1 个答案:

答案 0 :(得分:2)

您应该参考文件:

SESSION_ENGINE = 'python.path.mydb'

Django docs遗漏了这个小细节。在django/contrib/sessions/middleware.py第12行(django 1.9)中,您可以找到:

self.SessionStore = engine.SessionStore

所以它实际上是从settings. SESSION_ENGINE中提供的引擎中获取SessionStore类。