Django数据库设置

2024-04-19 11:41:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Django应用程序中有两个数据库,但是IP不同,ie主机也不同。在

    'default': { # This is the DB in the same machine itself.
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'sortation_gor',              
        'USER': 'vms_gor',
        'PASSWORD': 'apj0702',
        'HOST': '127.0.0.1',
        'PORT': '',
    },
    'a1': { # This is in another machine with IP listed below.
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'a1',              
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '192.168.1.27',
        'PORT': '',
    }
}

当我运行我的代码,并试图在a1DB中保存数据时,它返回我500 error。但是如果我把数据库放在运行应用程序的同一台机器上,它就可以正常工作了。有什么问题?

Q2)在我的模板中,我有一个检查字段:

^{pr2}$

虽然Configuration选项卡不可见,但即使我不是管理员,我仍然可以导航到该URL(前提是我知道确切的URL)。在

如何解决这一问题。

路由器.py文件

class GorRouter(object):
    """
    A router to control all database operations on models in the
    gor application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read gor models go to gor_db.
        """
        if model._meta.app_label == 'gor_data':
            return 'default'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write gor models go to gor_db.
        """
        if model._meta.app_label == 'gor_data':
            return 'default'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the gor app is involved.
        """
        if obj1._meta.app_label == 'gor_data' or \
           obj2._meta.app_label == 'gor_data':
           return True
        return None

    def allow_migrate(self, db, model):
        """
        Make sure the gor app only appears in the 'gor_db'
        database.
        """
        if db == 'default':
            return model._meta.app_label == 'gor_data'
        elif model._meta.app_label == 'gor_data':
            return False
        return None



class A1Router(object):
    def db_for_read(self, model, **hints):

        if model._meta.app_label == 'a1_data':
            return 'a1'
        return None

    def db_for_write(self, model, **hints):

        if model._meta.app_label == 'a1_data':
            return 'a1'
        return None

    def allow_relation(self, obj1, obj2, **hints):

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

    def allow_migrate(self, db, model):
        """
        Make sure the aramex app only appears in the 'aramex_db'
        database.
        """
        if db == 'a1':
            return model._meta.app_label == 'a1_data'
        elif model._meta.app_label == 'a1_data':
            return False
        return None

Tags: theinselfnoneappdbdatamodel