亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 django 的同一個應用程序中使用不同的數據庫?

如何在 django 的同一個應用程序中使用不同的數據庫?

守候你守候我 2021-11-09 18:30:44
我有一個名為 xyz 的應用程序,該應用程序中有 2 個視圖 view1.py 和 view2.py 我將路由器配置為if model._meta.app_label == 'xyz'     return database1有沒有辦法從此應用程序“xyz”中選擇不同的數據庫。我的意思是同一個應用程序中的兩個不同的數據庫。有沒有辦法或者django首先允許這樣做。
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

數據庫相關東西的配置主要在 settings.py 文件中完成。因此,要將多個數據庫添加到我們的 django 項目中,我們需要將它們添加到 DATABASES 字典中。


這些設置進去 Settings.py


DATABASE_ROUTERS = ['path.to.DemoRouter']

DATABASE_APPS_MAPPING = {'user_data': 'users_db',

                        'customer_data':'customers_db'}


DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

    },

    'users_db': {

        'NAME': 'user_data',

        'ENGINE': 'django.db.backends.postgresql',

        'USER': 'postgres_user',

        'PASSWORD': 'password'

    },

    'customers_db': {

        'NAME': 'customer_data',

        'ENGINE': 'django.db.backends.mysql',

        'USER': 'mysql_cust',

        'PASSWORD': 'root'

    }

}

對于多個數據庫,最好談談數據庫路由器。默認路由方案確保如果未指定數據庫,則所有查詢都回退到默認數據庫。數據庫路由器默認為 []。


把這個放進去 models.py


class DemoRouter:

    """

    A router to control all database operations on models in the

    user application.

    """

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

        """

        Attempts to read user models go to users_db.

        """

        if model._meta.app_label == 'user_data':

            return 'users_db'

        elif model._meta.app_label == 'customer_data':

            return 'customer_db'

        return None


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

        """

        Attempts to write user models go to users_db.

        """

        if model._meta.app_label == 'user_data':

            return 'users_db'

        elif model._meta.app_label == 'customer_data':

            return 'customer_db'

        return None


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

        """

        Allow relations if a model in the user app is involved.

        """

        if obj1._meta.app_label == 'user_data' or \

           obj2._meta.app_label == 'user_data':

           return True

        return None


    def allow_migrate(self, db, app_label, model_name=None, **hints):

        """

        Make sure the auth app only appears in the 'users_db'

        database.

        """

        if app_label == 'user_data':

            return db == 'users_db'

        return None

相應的模型將被修改為


class User(models.Model):

    username = models.Charfield(ax_length=100)

    . . .

        class Meta:

        app_label = 'user_data'


class Customer(models.Model):

    name = models.TextField(max_length=100)

    . . .

        class Meta:

        app_label = 'customer_data'

使用多個數據庫時,很少有有用的命令。


 $ ./manage.py migrate --database=users_db


查看完整回答
反對 回復 2021-11-09
  • 1 回答
  • 0 關注
  • 310 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號