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

时间:2016-02-03 14:46:44

标签: python django django-models django-admin

我有一个Django项目,为Django设置了默认数据库,但也需要访问旧数据库。我在设置和数据库路由器中工作。来自Django应用程序的模型对象本身出现在管理员中。但是,遗留数据库Django应用程序中的模型不会出现在管理员的权限部分下,我希望创建一个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',
        },
    },
}

我的路由器;遗留数据库应用程序被称为' formingitor':

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 Admin的权限部分中。这里有什么想法? Django Admin是否只能处理默认数据库中的模型,因为histitor和admin都在默认数据库中?我有谷歌并通过Django文档进行检查,但似乎无法找到明确的答案,我希望找到一个解决方案或者我错过的明显的东西。提前谢谢你抽出时间。

1 个答案:

答案 0 :(得分:0)

对我来说,我错过了“in_db”Meta param,但也许你的路由器不能像这样工作。 还要确保将路由器指向设置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'