For this example, we'll be modeling the following two entities connected by a many-to-many relationship:
We'll also use the following data:
Actors | Name | Date of Birth |
---|---|---|
Natalie Portman | 1981-06-09 | |
Orlando Bloom | 1977-01-13 | |
Johnny Depp | 1963-06-09 | |
Hugo Weaving | 1960-04-04 | |
Christopher Lee | 1922-05-27 |
Films | Name | Year | Actors |
---|---|---|---|
Star Wars: Episode II - Attack of the Clones | 2002 | Natalie Portman, Christopher Lee | |
Pirates of the Caribbean: The Curse of the Black Python | 2003 | Orlando Bloom, Johnny Depp | |
The Lord of the Rings: The Return of the King | 2003 | Orlando Bloom, Hugo Weaving, Christopher Lee | |
V for Vendetta | 2006 | Natalie Portman, Hugo Weaving |
In our current project, create a new application called
movies
. At the command line, type:
python manage.py startapp movies
Edit the sigcse/settings.py
file so that we can
connect to a sqlite3 database:
DATABASE_ENGINE = 'sqlite3' DATABASE_NAME = '/home/aortiz/sigcse/movies/movies.db' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_HOST = '' DATABASE_PORT = ''
Edit the sigcse/settings.py
file so that we can
use the Django administration site and our application's
database:
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'sigcse.movies', )
Edit the sigcse/urls.py
. Uncomment the highlighted
lines in order to define a URL for accessing the Django
administration site:
from django.conf.urls.defaults import * # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Example: # (r'^sigcse/', include('sigcse.foo.urls')), # Uncomment the admin/doc line below and add 'django.contrib.admindocs' # to INSTALLED_APPS to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: (r'^admin/(.*)', admin.site.root), (r'^hello/(\w+)/$', 'sigcse.hello.views.say'), (r'^fibonacci/(\d{1,4})/$', 'sigcse.fibonacci.views.do_it'), )
Define the application models. Add the following code to the
sigcse/movies/models.py
file:
from django.db import models class Actor(models.Model): name = models.CharField(max_length=30) date_of_birth = models.DateField() def __unicode__(self): return self.name class Film(models.Model): name = models.CharField(max_length=60) year = models.IntegerField() actors = models.ManyToManyField(Actor, related_name='films') def __unicode__(self): return self.name
Register the models that are to be managed using the
administration site. Create a file named admin.py
in the sigcse/movies
folder with the following
content:
from django.contrib import admin from sigcse.movies import models admin.site.register(models.Actor) admin.site.register(models.Film)
Synchronize the model definitions with the database. Type at the command line:
python manage.py syncdb
You'll be asked to create a superuser definition. Say yes, and type any additional requested information.
Add new information to the database using the Django shell. Type at the command line:
python manage.py shell
Type the following commands in the interactive shell in order to add two new instances of the Actor model:
>>> from sigcse.movies.models import Actor >>> from datetime import date >>> a1 = Actor(name='Natalie Portman', date_of_birth=date(1981, 6, 9)) >>> a1.save() >>> a2 = Actor(name='Orlando Bloom', date_of_birth=date(1977, 1, 13)) >>> a2.save()
The following commands demonstrate how to retrieve the stored information:
>>> r = Actor.objects.all() >>> r [<Actor: Natalie Portman>, <Actor: Orlando Bloom>] >>> a = r[0] >>> a <Actor: Natalie Portman> >>> a.id 1 >>> a.name u'Natalie Portman' >>> a.date_of_birth datetime.date(1981, 6, 9) >>> a = Actor.objects.get(name='Orlando Bloom') >>> a.id 2 >>> a.name u'Orlando Bloom' >>> a.date_of_birth datetime.date(1977, 1, 13)
Open the administration site. Using your web browser, visit this
following URL http://localhost:8000/admin/
and type
all the remaining actor and film information found at the
beginning of this page.
Once again, let's go to the interactive shell:
python manage.py shell
Let's now inspect programmatically the contents of the database:
>>> from sigcse.movies.models import Actor, Film >>> r = Film.objects.filter(name__contains="clones") >>> r [<Film: Star Wars: Episode II - Attack of the Clones>] >>> f = r[0] >>> r = f.actors.all() >>> r [<Actor: Natalie Portman>, <Actor: Christopher Lee>] >>> a = r[0] >>> r = a.films.all() >>> r [<Film: Star Wars: Episode II - Attack of the Clones>, <Film: V for Vendetta>]
Write a view/template in the current application that, given
a certain year (as part of the URL), displays the name of
all the films released that year (using an HTML unordered list).
For example, given the URL
http://localhost:8000/films/2003/
the output
should be:
In the current application, write a view/template that displays
in an HTML table all the actors stored in the database, sorted
alphabetically by name. Include also her or his date of birth,
as well as the names of all the films in which she or he has
starred. For example, given the URL
http://localhost:8000/actors/
the output should
look something like this: