Tuesday, January 31, 2012

Python & Django - Database setup

Database setup


Now, edit settings.py. It's a normal Python module with module-level variables representing Django settings. Change the following keys in the DATABASES 'default' item to match your databases connection settings.
§                                 ENGINE -- Either 'django.db.backends.postgresql_psycopg2', 'django.db.backends.mysql' or'django.db.backends.sqlite3'. Other backends are also available.
§                                 NAME -- The name of your database. If you're using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. If the file doesn't exist, it will automatically be created when you synchronize the database for the first time (see below).
When specifying the path, always use forward slashes, even on Windows (e.g.C:/homes/user/mysite/sqlite3.db).
§                                 USER -- Your database username (not used for SQLite).
§                                 PASSWORD -- Your database password (not used for SQLite).
§                                 HOST -- The host your database is on. Leave this as an empty string if your database server is on the same physical machine (not used for SQLite).
If you're new to databases, we recommend simply using SQLite (by setting ENGINE to'django.db.backends.sqlite3'). SQLite is included as part of Python 2.5 and later, so you won't need to install anything else.
Note
If you're using PostgreSQL or MySQL, make sure you've created a database by this point. Do that with "CREATE DATABASE database_name;" within your database's interactive prompt.
If you're using SQLite, you don't need to create anything beforehand - the database file will be created automatically when it is needed.
While you're editing settings.py, take note of the INSTALLED_APPS setting towards the bottom of the file. That variable holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.
By default, INSTALLED_APPS contains the following apps, all of which come with Django:
§                                 django.contrib.auth -- An authentication system.
§                                 django.contrib.contenttypes -- A framework for content types.
§                                 django.contrib.sessions -- A session framework.
§                                 django.contrib.sites -- A framework for managing multiple sites with one Django installation.
§                                 django.contrib.messages -- A messaging framework.
§                                 django.contrib.staticfiles -- A framework for managing static files.
These applications are included by default as a convenience for the common case.
Each of these applications makes use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:
python manage.py syncdb
The syncdb command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your settings.py file. You'll see a message for each database table it creates, and you'll get a prompt asking you if you'd like to create a superuser account for the authentication system. Go ahead and do that.
If you're interested, run the command-line client for your database and type \dt (PostgreSQL),SHOW TABLES; (MySQL), or .schema (SQLite) to display the tables Django created.
For the minimalists
Like we said above, the default applications are included for the common case, but not everybody needs them. If you don't need any or all of them, feel free to comment-out or delete the appropriate line(s) from INSTALLED_APPS before running syncdb. The syncdbcommand will only create tables for apps in INSTALLED_APPS.

No comments: