gpt4 book ai didi

python - 具有多个数据库的 Django,来自管理员中非默认数据库权限的模型

转载 作者:行者123 更新时间:2023-12-05 04:14:19 27 4
gpt4 key购买 nike

我有一个 Django 项目,其中为所有 Django 设置了默认数据库,但还需要访问遗留数据库。我在设置和数据库路由器中有这个工作。 Django 应用程序中的模型对象本身命中了旧数据库,出现在管理员中。但是,旧数据库 Django 应用程序中的模型不会出现在管理员的 permissions 部分下,我希望创建一个 Django 组,该组具有这些模型/表的权限,供员工执行 CRUD查找表上的函数。这是我的设置:

数据库:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mysite_classroom_d',
'USER': 'mysite_classroom_user',
'PASSWORD': '',
'HOST': 'mysite-pg1.institution.edu',
'PORT': '5432',
},
'mssqlmysite': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'sql14-dev.institution.edu',
'PORT': '1433',
'NAME': 'mysite',
'USER': 'mysite_user',
'PASSWORD': '',
'AUTOCOMMIT': True,
'OPTIONS': {
'driver': 'FreeTDS',
'autocommit': True,
'unicode_results': True,
'host_is_server': True,
'extra_params': 'tds_version=7.2',
},
},
}

还有我的路由器;遗留数据库应用程序称为“formeditor”:

class FormEditorRouter(object):
"""
A router to control all database operations on models in the
formeditor application.
"""

def db_for_read(self, model, **hints):
"""
Attempts to read formeditor models go to mssqlmysite.
"""

if model._meta.app_label == 'formeditor':
return 'mssqlmysite'
return None


def db_for_write(self, model, **hints):
"""
Attempts to write formeditor models go to mssqlmysite.
"""

if model._meta.app_label == 'formeditor':
return 'mssqlmysite'
return None


def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the formeditor app is involved.
"""

if obj1._meta.app_label == 'formeditor' or \
obj2._meta.app_label == 'formeditor':
return True
return None


def allow_migrate(self, db, app_label, model=None, **hints):
"""
Make sure the formeditor app only appears in the 'mssqlmysite'
database.
"""

if app_label == 'formeditor':
return db == 'mssqlmysite'
return None

还有一个示例模型:

class DataCompanyMap(models.Model):
vendor_id = models.IntegerField(blank=True, null=True)
product_id = models.IntegerField(blank=True, null=True)
file_id = models.IntegerField(blank=True, null=True)
var_name = models.TextField(blank=True)
common_var_name = models.TextField()
searchable = models.NullBooleanField()
example = models.TextField(blank=True)
description = models.TextField(blank=True)

class Meta:
managed = False
db_table = 'data_company_map'

我认为问题可能出在 managed = False 上,但即使将遗留数据库模型更改为 managed = True 也不会使其出现在Django 管理员。这里有什么想法吗? Django Admin 能否只处理默认数据库中的模型,因为 forditor 和 admin 在默认数据库中?我用谷歌搜索并查看了 Django 文档,但似乎无法找到明确的答案,我希望能找到解决方案或我错过的明显问题。预先感谢您抽出宝贵时间。

最佳答案

对我来说,我没有缺少“in_db”元参数,但也许您的路由器不能像这样工作。还要确保将路由器指向设置 DATABASE_ROUTERS

它对我有用的解决方案:

我的路由器:(来自:https://djangosnippets.org/snippets/2687/)

class ModelDatabaseRouter(object):
"""Allows each model to set its own destiny"""

def db_for_read(self, model, **hints):
# Specify target database with field in_db in model's Meta class
if hasattr(model._meta, 'in_db'):
return model._meta.in_db
return None

def db_for_write(self, model, **hints):
# Specify target database with field in_db in model's Meta class
if hasattr(model._meta, 'in_db'):
return model._meta.in_db
return None

def allow_syncdb(self, db, model):
# Specify target database with field in_db in model's Meta class
if hasattr(model._meta, 'in_db'):
if model._meta.in_db == db:
return True
else:
return False
else:
# Random models that don't specify a database can only go to 'default'
if db == 'default':
return True
else:
return False

在设置中添加:

DATABASE_ROUTERS = ['api.routers.ModelDatabaseRouter']

然后是模型:

class DataCompanyMap(models.Model):

..... The fields ....

class Meta:
in_db = 'mssqlmysite'

关于python - 具有多个数据库的 Django,来自管理员中非默认数据库权限的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com