【问题标题】:Django with Multiple Databases, Models from non-default Database Permissions in Admin具有多个数据库的 Django,来自管理员中非默认数据库权限的模型
【发布时间】:2016-05-12 19:58:01
【问题描述】:

我有一个 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 管理员的权限部分中。这里有什么想法吗?由于formeditor和admin在默认数据库中,Django Admin是否只能处理默认数据库中的模型?我已经通过谷歌搜索并检查了 Django 文档,但似乎找不到明确的答案,我希望找到一个解决方案或我错过的明显的东西。提前感谢您抽出宝贵时间。

【问题讨论】:

    标签: python django django-models django-admin


    【解决方案1】:

    对我来说,我缺少“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'  
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      相关资源
      最近更新 更多