Django 1.3 overview

>>Django at a glance

Because Django was developed in a fast-paced newsroom environment, it was designed to make common Web-development tasks fast and easy. Here’s an informal overview of how to write a database-driven Web app with Django.

The goal of this document is to give you enough technical specifics to understand how Django works, but this isn’t intended to be a tutorial or reference – but we’ve got both! When you’re ready to start a project, you can start with the tutorial or dive right into more detailed documentation.

>Design your model
Although you can use Django without a database, it comes with an object-relational mapper in which you describe your database layout in Python code.

The data-model syntax offers many rich ways of representing your models – so far, it’s been solving two years’ worth of database-schema problems. Here’s a quick example, which might be saved in the file

mysite/news/models. py:

Class Reporter(models. Model):
full_name = models. CharField(max_length=70)

Def __unicode__(self):
return self. full_name

Class Article(models. Model):
pub_date = models. DateTimeField()
headline = models. CharField(max_length=200)
content = models. TextField()
reporter = models. ForeignKey(Reporter)

Def __unicode__(self):
return self. headline
Install it

Next, run the Django command-line utility to create the database tables automatically:
Manage. py syncdb

The syncdb command looks at all your available models and creates tables in your database for whichever tables don’t already exist.

>Enjoy the free API
With that, you’ve got a free, and rich, Python API to access your data. The API is created on the fly, no code generation necessary:

# Import the models we created from our “news” app
>>> from news. models import Reporter, Article

# No reporters are in the system yet.
>>> Reporter. objects. all()
[]

# Create a new Reporter.
>>> r = Reporter(full_name=’John Smith’)

# Save the object into the database. You have to call save() explicitly.
>>> r. save()

# Now it has an ID.
>>> r. id
1

# Now the new reporter is in the database.
>>> Reporter. objects. all()
[]

# Fields are represented as attributes on the Python object.
>>> r. full_name
‘John Smith’

# Django provides a rich database lookup API.
>>> Reporter. objects. get(id=1)

>>> Reporter. objects. get(full_name__startswith=’John’)

>>> Reporter. objects. get(full_name__contains=’mith’)

>>> Reporter. objects. get(id=2)
Traceback (most recent call last):

DoesNotExist: Reporter matching query does not exist.

# Create an article.
>>> from datetime import datetime
>>> a = Article(pub_date=datetime. now(), headline=’Django is cool’,
… content=’Yeah.’, reporter=r)
>>> a. save()

# Now the article is in the database.
>>> Article. objects. all()
[]

# Article objects get API access to related Reporter objects.
>>> r = a. reporter
>>> r. full_name
‘John Smith’

# And vice versa: Reporter objects get API access to Article objects.
>>> r. article_set. all()
[]

# The API follows relationships as far as you need, performing efficient
# JOINs for you behind the scenes.
# This finds all articles by a reporter whose name starts with “John”.
>>> Article. objects. filter(reporter__full_name__startswith=”John”)
[]

# Change an object by altering its attributes and calling save().
>>> r. full_name = ‘Billy Goat’
>>> r. save()

# Delete an object with delete().
>>> r. delete()

>A dynamic admin interface: it’s not just scaffolding – it’s the whole house



Django 1.3 overview