For marketers
who love technology
Home » , , » Discovering Django on Debian - part 3: creating the models (example of a text processing web app)

Discovering Django on Debian - part 3: creating the models (example of a text processing web app)

Django logo
Django logo (Photo credit: Wikipedia)
Have a look to the first two parts of this serie of Django tutorials, to see how to install virtualenv and django, and how to configure django with a mysql database.

First a bit of vocabulary. We have seen that Django creates a package that contains the configuration of our website and various management scripts. The content of our website will be a single web application that will process a user's input submitted through a form. Let's initialize this app:

python startapp textModif

This command will create a folder (a python package) named textModif.

geany textModif/*.py &

The most important part of our website's design comes now. We need to identify the pieces of data that we will manipulate in our web app. This is called the model of our web app. We edit and write:

from django.db import models

# Create your models here.
class TextTask(models.Model):
    description = "A query submitted to the web app and the result of the web app operations"
    originalText = models.CharField(max_length=10000)
    finalText = models.CharField(max_length=10000)
    task_date = models.DateTimeField('date requested')
    def __unicode__(self):
        return self.originalText

class TextOperation(models.Model):
    description = "A type of operation the web app did on the submitted text"
    modifType = models.CharField(max_length=1000)
    modifDescription = models.CharField(max_length=1000)
    def __unicode__(self):
        return self.modifTypeclass TextModification(models.Model):
    description = "A specific change the web add did on the text. For instance, delete a word in a sentence."
    task = models.ForeignKey(TextTask)
    operation = models.ForeignKey(TextOperation)
    originalText = models.CharField(max_length=1000)
    finalText = models.CharField(max_length=1000)
    def __unicode__(self):
        return self.originalText

Now we can indicate to Django that we want to use our web app in the scope of the django project on which we work (that I named "djangoProj_1"). To do this, we edit the of our project and add:


We request Django to create the tables in our database in accordance to our model:

python sql textModif

If everything looks ok; we can commit the SQL configuration:

python syncdb

This commands configures mysql and also creates apis so that we can access the database conveniently through python instructions. Let's try a few things as in Django's official tutorial. We first launch django's python shell (it's configured with appropriate environment variables)

python shell

Then, let's play

from textModif.models import *
from django.utils import timezone
# let's list our tasks (none exists for the moment)
        finalText = "Queen, you order to re-open cruel injuries",
        task_date =
# let's list our tasks in the database (none exists for the moment as we have not committed our changes to mysql)
# let's see the id of our object in the database

Now our item is correctly saved in the database:

>>> TextTask.objects.all()

Note that in the list, our object is described through a call to the "__unicode__" method that we have defined in


About Gilles


Post a Comment