Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

python - Using existing database in Django

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 database, with existing data, which I plan to use for the content generation.

I wanted the classes in models.py to link up to the existing tables in vo. However, when I do

manage.py syncdb --database=vo

a set of new empty tables are created. BTW, it's a SQLite3 database.

How does one link existing tables in a database to the classes on models.py in Django?

Many thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...