Django项目中有2个SQL Server数据库

时间:2019-02-20 14:19:31

标签: sql-server django django-settings

我在将数据加载到database1中时遇到问题(默认)。您会看到,系统只需要加载database2(源)中的数据。该系统在我的悔者的机器上工作,但是它加载了两个不同的端口并使用docker,我安装了SQL Server。系统启动,问题是当我想在数据库1中加载数据时,它告诉我该数据在数据库2中不存在,然后就不存在。现在,如果我尝试加载数据库2中没有的数据(如果正确加载)。我搜索了如何更改SQL Server的端口,但没有得到。谁能帮我吗?

DATABASES = {
        'default': {
            'ENGINE': 'sql_server.pyodbc',
            'NAME': 'database1',
            'HOST': 'name\\name',
            'PORT': '',
            'USER': 'user1',
            'PASSWORD': 'password1',
            'OPTIONS': {
                'driver': 'ODBC Driver 13 for SQL Server',
            }
        },
        'source': {
            'ENGINE': 'sql_server.pyodbc',
            'NAME': 'database2',
            'HOST': 'name\\name',
            'PORT': '',
            'USER': 'user2',
            'PASSWORD': 'password2',
            'OPTIONS': {
                'driver': 'ODBC Driver 13 for SQL Server',
            }
        }

1 个答案:

答案 0 :(得分:0)

这是配置: def decide_on_model(model): """Small helper function to pipe all DB operations of a worlddata model to the world_data DB""" return 'source' if model._meta.app_label == 'source' else None

class TutoriasRouter: """ Implements a database router so that:

* Django related data - DB alias `default` - MySQL DB `world_django`
* Legacy "world" database data (everything "non-Django") - DB alias `world_data` - MySQL DB `world`
"""
def db_for_read(self, model, **hints):
    return decide_on_model(model)

# def db_for_write(self, model, **hints):
#     return decide_on_model(model)

def db_for_write(self, model, **hints):
    return 'default'

def allow_relation(self, obj1, obj2, **hints):
    # Allow any relation if both models are part of the worlddata app
    # if obj1._meta.app_label == 'source' and obj2._meta.app_label == 'source':
    #     return True
    # # Allow if neither is part of worlddata app
    # elif 'source' not in [obj1._meta.app_label, obj2._meta.app_label]:
    #     return True
    # # by default return None - "undecided"
    return True

def allow_migrate(self, db, app_label, model_name=None, **hints):
    # allow migrations on the "default" (django related data) DB
    if db == 'default' and app_label != 'source':
        return True

    # allow migrations on the legacy database too:
    # this will enable to actually alter the database schema of the legacy DB!

    # if db == 'source' and app_label == "source":
    #    return True

    return False

`