Home > Software engineering >  Connection to the another DB not created by Django
Connection to the another DB not created by Django

Time:01-24

I have Django project and want to look the another db (not default db created by Php Symfony)

Django can set up two DB in settins.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        "NAME": config("DB_NAME"),
        "USER": config("DB_USER"),
        "PASSWORD": config("DB_PASSWORD"),
        "HOST": config("DB_HOST"),
        "PORT": config("DB_PORT"),
        'OPTIONS': {
            'charset': 'utf8mb4',
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
        },
    }

    'extern': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': config("DB_EXTERN_NAME"),
        'USER': config("DB_EXTERN_USER"),
        'PASSWORD': config("DB_EXTERN_PASSWORD"),
        'HOST': config("DB_EXTERN_HOST"),
        'PORT': config("DB_EXTERN_PORT"),
    }
}

and set models.py for example

class Area(models.Model):
    class Meta:
        db_table = 'my_area'

However it requires to change the extern database when python manage.py makemigrations,python manage.py migrate

I just want to reference the extern db not alter.

Is there any good practice??

CodePudding user response:

You may be looking for the managed = false meta setting on your models. That will cause Django not to try to manage those models (such as creating migrations for them). It's commonly used when working with externally managed databases.

EG:

class Area(models.Model):
    class Meta:
        db_table = 'my_area'
        managed = false

CodePudding user response:

I think what you need do is run

python manage.py migrate --database extern
  •  Tags:  
  • Related