This document summarizes the evolution of Django's model syntax from its early beginnings in 2004 to the present day trunk version. It shows the creation of database tables and models for a polls application. The model syntax progressed from raw SQL, to a custom Model class, to utilizing Django's built-in Model and fields, and later the introduction of the Meta inner class. It ends by showing how models are now registered with the admin using a separate admin.py file.
3. cms/apps/polls/dblayout.sql
BEGIN;
CREATE TABLE polls (
id serial PRIMARY KEY,
label varchar(20) NOT NULL, -- used by the template and possibly by the URL
release_date timestamp with time zone NOT NULL,
expire_date timestamp with time zone NOT NULL,
question varchar(255) NOT NULL
);
CREATE INDEX polls_label ON polls (label);
CREATE TABLE poll_choices (
id serial PRIMARY KEY,
polls_id integer REFERENCES polls(id),
choice varchar(255) NOT NULL,
votes integer NOT NULL DEFAULT 0
);
CREATE TABLE poll_logged_votes (
poll_id integer NOT NULL REFERENCES polls(id),
choice_id integer NOT NULL REFERENCES poll_choices(id),
ip_address inet NOT NULL,
vote_time timestamp with time zone NOT NULL,
PRIMARY KEY (poll_id, ip_address, vote_time)
);
CREATE TABLE polls_sites (
polls_id integer NOT NULL REFERENCES polls(id),
sites_id integer NOT NULL REFERENCES sites(id),
PRIMARY KEY (polls_id, sites_id)
);
COMMIT;