Thursday, October 15, 2009

Django - Design Your Model

The first step that I'm going to take to designing my web app is to start building up the model. Reference for this section starts with the Design Your Model section of the Django overview, and also the related section Data-Model Syntax.

The model is one of three parts that build up the Model-View-Controller architectural pattern. Basically, the model is a description of your data. Let's say you're designing a social networking application. In plain English, your model would start off something like, "The basic unit of data that we are working with is a member of the site. A member has a first name, last name, password and chosen user name. Members are related to other members in a many-to-many relationship. Members can be friends with other members, they can ignore other members, etc. By default, a member's list of friends is sorted by the order in which they were added..." and your model would continue. The formal model is nothing more than a translation of this natural language model into python.

The view component of an MVC web app tells the browser how pages are displayed. Mostly, the view is made up of templates that have components of the model plugged in.

The controller of an MVC web app handles page requests and decides what to do with components of the model. For example, if you went to visit the home page of our social networking example, it would check to see if you had an existing session, and if not, it will grab a log in page provided by the view. After a user enters this info, the controller takes care of several steps:
  • It attempts to authenticate the user against existing users in the database
  • If authentication is successful, it grabs the appropriate components of the model
  • It plugs those components into a template provided by the view
  • It sends the template, complete with info composed from the database, to the user's browser.
Armed with this information, I am going to begin writing parts of the model, and then write some python scripts to import my existing data into my Django web app.

For further reference, see Interacting with a Database: Models from Definitive Guide to Django; however, please note that, although this text is less than two years old, different versions of Django can change dramatically. The most up-to-date reference is always available at http://www.djangoproject.com/.

No comments:

Post a Comment