==================
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. Please see our more-detailed Django documentation_ when you're ready
to start a project.
.. _documentation: http://www.djangoproject.com/documentation/
Design your model
=================
Start by describing your database layout in Python code. Django's data-model API
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::
class Reporter(meta.Model):
fields = (
meta.CharField('full_name', "reporter's full name", maxlength=70),
)
def __repr__(self):
return self.full_name
class Article(meta.Model):
fields = (
meta.DateTimeField('pub_date', 'publication date'),
meta.CharField('headline', 'headline', maxlength=200),
meta.TextField('article', 'article'),
meta.ForeignKey(Reporter),
)
def __repr__(self):
return self.headline
Install it
==========
Next, run the Django command-line utility. It'll create the database tables for
you automatically, in the database specified in your Django settings. Django
works with PostgreSQL and MySQL, although other database adapters are on the
way::
django-admin.py install news
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::
# Modules are dynamically created within django.models.
# Their names are plural versions of the model class names.
>>> from django.models.news import reporters, articles
# No reporters are in the system yet.
>>> reporters.get_list()
[]
# Create a new Reporter.
>>> r = reporters.Reporter(id=None, 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.
>>> reporters.get_list()
[John Smith]
# Fields are represented as attributes on the Python object.
>>> r.full_name
'John Smith'
# Django provides a rich database lookup API that's entirely driven by keyword arguments.
>>> reporters.get_object(id__exact=1)
John Smith
>>> reporters.get_object(full_name__startswith='John')
John Smith
>>> reporters.get_object(full_name__contains='mith')
John Smith
>>> reporters.get_object(id__exact=2)
Traceback (most recent call last):
...
django.models.news.ReporterDoesNotExist: Reporter does not exist for {'id__exact': 2}
# Create an article.
>>> from datetime import datetime
>>> a = articles.Article(id=None, pub_date=datetime.now(), headline='Django is cool', article='Yeah.', reporter_id=1)
>>> a.save()
# Now the article is in the database.
>>> articles.get_list()
[Django is cool]
# Article objects get API access to related Reporter objects.
>>> r = a.get_reporter()
>>> r.full_name
'John Smith'
# And vice versa: Reporter objects get API access to Article objects.
>>> r.get_article_list()
[Django is cool]
# The API follows relationships as far as you need.
# Find all articles by a reporter whose name starts with "John".
>>> articles.get_list(reporter__full_name__startswith="John")
[Django is cool]
# 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
============================================================================
Once your models are defined, Django can automatically create an administrative
interface -- a Web site that lets authenticated users add, change and
delete objects. It's as easy as adding an extra admin attribute to your model
classes::
class Article(meta.Model):
fields = (
meta.DateTimeField('pub_date', 'publication date'),
meta.CharField('headline', 'headline', maxlength=200),
meta.TextField('article', 'article'),
meta.ForeignKey(Reporter),
)
admin = meta.Admin(
fields = (
(None, {'fields': ('headline', 'article')}),
('Extra stuff', {'fields': ('pub_date', 'reporter_id')}),
),
)
The ``admin.fields`` defines the layout of your admin form. Each element in the
fields tuple corresponds to a ``