Python Django Tutorial 2 – settings.py and database models

1. Setup settings.py

By default, it’s automatically setup to use SqlLite with the default db being db.sqlite3 file created in your django_test application directory created in Tutorial 1.

Set Language Code and Time Zone

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Los_Angeles'

2. Create a new App inside the project
Navigate to the django_test directory and create a new app called article.

python manage.py startapp article

An empty file called __init__.py is created in the article directory. This tells python that article is a package that can be imported as a module. The other files that are created are views.py, tests.py and models.py. For now we will concentrate only on models.py.

3. Create models
Create a simple model for the article app in models.py

from django.db import models

# Create your models here.
class Article(models.Model):
	title = models.CharField(max_length=200)
	body = models.TextField()
	pub_date = models.DateTimeField('date published')
	likes = models.IntegerField()

4. Syncdb
Execute syncdb to build django tables and to setup the superuser.

python manage.py syncdb

Deprecated since version 1.7:
This command has been deprecated in favor of the migrate command, which performs both the old behavior as well as executing migrations. It is now just an alias to that command.

5. Add application to INSTALLED_APPS
You’ll soon realize that syncdb did not create the tables for the class defined in model.py. This is because the app isn’t listed under INSTALLED_APPS yet. In order to do that, open the settings.py file and add the following line to it:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'article',
)

6. Run migrations
After running syncdb, you’ll get a warning to use migrations. Use the following command to create and apply migrations

$ python manage.py makemigrations
Output:
Migrations for 'article':
  0001_initial.py:
    - Create model Article

$ python manage.py migrate
Output:
Running migrations:
  Rendering model states... DONE
  Applying article.0001_initial... OK

If you ever wanted to drop the application tables entirely from DB, run the following command:
python manage.py reset article

7. Using shell
To access the data, you can use the manage.py shell command to view, add or alter data.

8. Use the __unicode__ method to make the object data more meaningful

from django.db import models

# Create your models here.
class Article(models.Model):
	title = models.CharField(max_length=200)
	body = models.TextField()
	pub_date = models.DateTimeField('date published')
	likes = models.IntegerField()

	def __unicode__(self):
		return self.title
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s