在 django 1.11 中,如何允许用户登录只读数据库?
In django 1.11, how to allow users to login on a read-only database?
我有 2 个实例,每个实例 运行 都有自己的 postgres 数据库。
一个用于生产用途。另一个是从生产数据库执行复制的只读数据库。
两个实例 运行 相同的 django 1.11 应用程序代码库。
当我尝试登录 django 只读版本时,我无法登录,因为登录的动作显然执行了一些更新或插入语句。
我收到有关只读数据库的内部错误:cannot execute INSERT in a read-only transaction
如果我想允许用户使用相同的代码库访问只读数据库,我有什么选择?
更新
我已经试过了django-postgres-readonly
。相同的结果。
Django 需要像 django_session
.
一样更新表
我的建议是为 "django-tables" 和您的 "read-only-tables"
使用 2 个不同的数据库
如何?
创建一个简单的空 sqlite3 数据库并使用 class AuthRouter
用于管理它们。
对于您的数据库设置,请使用类似的东西:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'otherdb': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'ypurusername',
'PASSWORD': 'yourpassword',
'HOST': '0.0.0.0'
}
}
AuthRouter 示例:
class AuthRouter:
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.db_table == 'django-table':
return 'defaul'
return otherdb
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if app_label == 'migrations':
return db == 'default'
return otherdb
Here link 到文档
在与 read-only 数据库通信的代码库上
第 1 步:安装 django-no-last-login v0.1.0
第 2 步:在 settings.py add/change 里面
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
INSTALLED_APPS += [
'nolastlogin',
]
NO_UPDATE_LAST_LOGIN = True
默认情况下,Django 使用数据库作为会话引擎,所以切换到其他引擎。
此外,该插件还可以轻松关闭 Django 更新上次登录行为。
Django 自动更新上次登录时间。因为我们想要零数据库写入,所以我们需要使用它。
我有 2 个实例,每个实例 运行 都有自己的 postgres 数据库。
一个用于生产用途。另一个是从生产数据库执行复制的只读数据库。
两个实例 运行 相同的 django 1.11 应用程序代码库。
当我尝试登录 django 只读版本时,我无法登录,因为登录的动作显然执行了一些更新或插入语句。
我收到有关只读数据库的内部错误:cannot execute INSERT in a read-only transaction
如果我想允许用户使用相同的代码库访问只读数据库,我有什么选择?
更新
我已经试过了django-postgres-readonly
。相同的结果。
Django 需要像 django_session
.
我的建议是为 "django-tables" 和您的 "read-only-tables"
使用 2 个不同的数据库如何?
创建一个简单的空 sqlite3 数据库并使用 class AuthRouter
用于管理它们。
对于您的数据库设置,请使用类似的东西:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'otherdb': {
'NAME': 'user_data',
'ENGINE': 'django.db.backends.postgresql',
'USER': 'ypurusername',
'PASSWORD': 'yourpassword',
'HOST': '0.0.0.0'
}
}
AuthRouter 示例:
class AuthRouter:
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.db_table == 'django-table':
return 'defaul'
return otherdb
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label == 'auth':
return 'auth_db'
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if app_label == 'migrations':
return db == 'default'
return otherdb
Here link 到文档
在与 read-only 数据库通信的代码库上
第 1 步:安装 django-no-last-login v0.1.0
第 2 步:在 settings.py add/change 里面
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
INSTALLED_APPS += [
'nolastlogin',
]
NO_UPDATE_LAST_LOGIN = True
默认情况下,Django 使用数据库作为会话引擎,所以切换到其他引擎。
此外,该插件还可以轻松关闭 Django 更新上次登录行为。
Django 自动更新上次登录时间。因为我们想要零数据库写入,所以我们需要使用它。