Transferring the Database
As mentioned at the beginning of the chapter, we created Hyperlink and Favorite models, and we created objects of each. Rather than repeat all that work, we'll copy the database file, favoritesdb, that was created in Chapter 3.
The copy process involves these steps:
- Copy favoritesdb.
- Copy models.py.
- Edit settings.py to create the database favoritesdb.
- Synchronize the database.
To transfer the database:
- Copy the database, favoritesdb, from the chapter3 folder to the chapter4 folder. This is the big step, because the favoritesdb database contains the tables and data that we'll use in this chapter.
- Copy models.py from chapter3\favorites to chapter4\favorites. (If your operating system asks whether you want to replace the existing models.py, answer Yes.)
The models.py file contains the definition of the models you want to use. Unless you copy the version of models.py created in Chapter 3 that defines the models, you'll lose the models in the database when you synchronize the database.
- Using a text-editing program, open chapter4\settings.py and find the database section (Listing 4.3).
Listing 4.3. The settings.py file.
DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. DATABASE_NAME = '' # Or path to database file if using sqlite3. DATABASE_USER = '' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3.
- Enter
'sqlite3'
for the DATABASE_ENGINE setting and
favoritesdb
for the DATABASE_NAME setting (Listing 4.4).
Listing 4.4. The edited settings.py file.
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. DATABASE_NAME = 'favoritesdb' # Or path to database file if using sqlite3. DATABASE_USER = '' # Not used with sqlite3. DATABASE_PASSWORD = '' # Not used with sqlite3.
- Save the file.
- Open a command prompt, and in the chapter4 directory, enter this command to set up the database system:
$ python manage.py syncdb
You will not be prompted to create a superuser account, since that account already exists in the chapter3 favoritesdb database.
Excellent—the database is set up, having been transferred from the chapter3 project.