Django Models

The MTV (or MVC) Development Pattern

  • Model: Refers to the data access layer. This is handled by Django's database layer.
  • View: Refers to the part of he system that selects what to display and how to display it. This is handled by views and templates.
  • Controller: Refers to the port of the system that decides which view to use, depending on user input, accessing the model as needed. This is handled by the framework itself by following your URLconf and calling the appropriate Python function for the given URL.

Django has been referred to as an MTV framework. In the MTV development pattern,

  • M stands for Model, the data access layer. This layer contains anything and everything about the data: how to access it, how to validate it, which behaviors it has, and the relationships between the data.
    • T stands for Template, the presentation layer. This layer contains presentation-related decisions: how something should be displayed on a Web page or other type of document.
    • V stands for View, the business logic layer. This layer contains the logic that access the model and defers to the appropriate template(s).

Configuring the Database

Database configuration is in settings.py.

Defining Models in Python

A Django model is a description of the data in your database, represented as Python code.

Example

  • An author has a first name, a last name and an email address.
  • A publisher has a name, a street address, a city, a state/province, a country, and a Web site.
  • A book has a title and a publication date. It also has one or more authors (a many-to-many relationship with authors) and a single publisher (a one-to-many relationship — aka foreign key — to publishers).
from django.db import models
 
class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()
 
class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
 
class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()
  • We haven’t explicitly defined a primary key in any of these models. Unless you instruct it otherwise, Django automatically gives every model an auto-incrementing integer primary key field called id. Each Django model is required to have a single-column primary key.

Installing the Model

  • First, we have to activate these models in our Django project. This is done by adding the books app to the list of installed apps in the settings file. Add the following lines:
INSTALLED_APPS = (
    # 'django.contrib.auth',
    # 'django.contrib.contenttypes',
    # 'django.contrib.sessions',
    # 'django.contrib.sites',
    'mysite.books',
)
  • To validate the models run the command: python manage.py validate
  • To generate the CREATE TABLE statements run the following command: python manage.py sqlall books
  • The sqlall command doesn’t actually create the tables or otherwise touch your database — it just prints output to the screen so you can see what SQL Django would execute if you asked it.
  • To commit the SQL to the database run python manage.py syncdb
  • To save an object use the save() method.
  • To create an object and save it to the database use the objects.create() method.
Bibliography
page_revision: 2, last_edited: 1259068712|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.