Using existing database in Django

前端 未结 1 1655
忘掉有多难
忘掉有多难 2020-12-13 16:37

I\'m using multiple databases in a Django app. The default database is the one Django creates for user authentication, etc. Then I have a vo datab

相关标签:
1条回答
  • 2020-12-13 16:59
    1. You need to add your db vo to settings.

    if you have your database settings like this

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', 
            'NAME': os.path.join(DIR, 'django.sqlite3'),
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
        },
    }
    

    Add vo database settings to it like this

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', 
            'NAME': os.path.join(DIR, 'django.sqlite3'),
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
        },
    
        # this your existing db 
        'vo': { 
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(DIR, 'vo.sqlite'),
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
        },
    }
    
    1. Then you can generate models automatically from the database.

      $ ./manage.py inspectdb --database=vo > your_app/models.py
      
    2. Configure database routers.

    Check out: Using Django's Multiple Database Support

    0 讨论(0)
提交回复
热议问题