===============
Model reference
===============
A model is the single, definitive source of data about your data. It contains
the essential fields and behaviors of the data you're storing. Generally, each
model maps to a single database table.
The basics:
* Each model is a Python class that subclasses ``django.db.models.Model``.
* Each attribute of the model represents a database field.
* Model metadata (non-field information) goes in an inner class named
``Meta``.
* Metadata used for Django's admin site goes into an inner class named
``Admin``.
* With all of this, Django gives you an automatically-generated
database-access API, which is explained in the `Database API reference`_.
A companion to this document is the `official repository of model examples`_.
(In the Django source distribution, these examples are in the
``tests/modeltests`` directory.)
.. _Database API reference: http://www.djangoproject.com/documentation/db_api/
.. _official repository of model examples: http://www.djangoproject.com/documentation/models/
Quick example
=============
This example model defines a ``Person``, which has a ``first_name`` and
``last_name``::
from django.db import models
class Person(models.Model):
first_name = models.CharField(maxlength=30)
last_name = models.CharField(maxlength=30)
``first_name`` and ``last_name`` are *fields* of the model. Each field is
specified as a class attribute, and each attribute maps to a database column.
The above ``Person`` model would create a database table like this::
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);
Some technical notes:
* The name of the table, ``myapp_person``, is automatically derived from
some model metadata but can be overridden. See _`Table names` below.
* An ``id`` field is added automatically, but this behavior can be
overriden. See `Automatic primary key fields`_ below.
* The ``CREATE TABLE`` SQL in this example is formatted using PostgreSQL
syntax, but it's worth noting Django uses SQL tailored to the database
backend specified in your `settings file`_.
.. _settings file: http://www.djangoproject.com/documentation/settings/
Fields
======
The most important part of a model -- and the only required part of a model --
is the list of database fields it defines. Fields are specified by class
attributes.
Example::
class Musician(models.Model):
first_name = models.CharField(maxlength=50)
last_name = models.CharField(maxlength=50)
instrument = models.CharField(maxlength=100)
class Album(models.Model):
artist = models.ForeignKey(Musician)
name = models.CharField(maxlength=100)
release_date = models.DateField()
num_stars = models.IntegerField()
Field name restrictions
-----------------------
Django places only two restrictions on model field names:
1. A field name cannot be a Python reserved word, because that would result
in a Python syntax error. For example::
class Example(models.Model):
pass = models.IntegerField() # 'pass' is a reserved word!
2. A field name cannot contain more than one underscore in a row, due to
the way Django's query lookup syntax works. For example::
class Example(models.Model):
foo__bar = models.IntegerField() 'foo__bar' has two underscores!
These limitations can be worked around, though, because your field name doesn't
necessarily have to match your database column name. See `db_column`_ below.
SQL reserved words, such as ``join``, ``where`` or ``select``, *are* allowed as
model field names, because Django escapes all database table names and column
names in every underlying SQL query. It uses the quoting syntax of your
particular database engine.
Field types
-----------
Each field in your model should be an instance of the appropriate ``Field``
class. Django uses the field class types to determine a few things:
* The database column type (e.g. ``INTEGER``, ``VARCHAR``).
* The widget to use in Django's admin interface, if you care to use it
(e.g. ````, ``