How to access more databases in Django using SQLite 3
In Django I am using two applications:
- python manage.py startapp books
- python manage.py startapp contacts
Still I am only using the books application models. So I am using DATABASE_NAME
like:
DATABASE_NAME = 'C:/WorkBase/P开发者_开发技巧ython/first/books/book.db'
Now I want to use the second one contacts application models. How can I add contact application models to DATABASE_NAME?
My INSTALLED_APPS
looks like this.
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'first.books',
'first.contacts',
)
Note : I am using SQLite 3 as the database.
I think what Aravind is asking is "How do I create the models for my contacts app in the books.db sqlite3 database?". Most people seem to think he wants multiple db support - but that doesn't appear to be the case.
1) The name of the database file is irrelevant. Just because you called it books doesn't mean that it won't store models from all the apps in your INSTALLED_APPS.
2) To create the models for the Contacts app in the sqlite3 database you will have to do a manage.py syncdb - any new tables will be created for you, modifications of the model may require you to delete the books.db file before doing the manage.py syncdb.
Just as an example: django.contrib.auth
has created tables in the books.db database file. So did any other application listed in your INSTALLED_APPS.
Hope that helps.
Django doesn't currently support multiple databases. However you can just syncdb your new models in your new applications and they will all go in the same database and everything will work.
In SQLITE you can "Attach Database" to another SQLite database.
I am not sure of the exact syntax as I have always done this via the sqliteman "System" menu.
The attached database table uses a different "schema"/high level qualifier from the local tables but that shouldn't be a problem for Django.
精彩评论