In the preceding issues of this newsletter, we have considered the basics of data modelling, data types, connections, identifiers and many other things.  One point we have kept returning to is a fundamental rule of data modelling:

Don’t make anything any more complex than you have to.

Or to put it another way:

Don’t store anything you don’t need.

Or even:

Keep it simple, stupid.

We finished that section with a simplified case study, modelling the EPANET input file format with Hezekiah’s tunnel providing the sample data.

Now we begin a series of articles on Django, focusing on the modelling side of this wonderful tool.  The plan is to go through the same sorts of modelling questions with Django as we visited in previous articles, considering the same problems and questions.  However, this time, we will get our answers using Django, and highlight any differences that result.

Intro to Django

Before looking at our first example, though, let’s just give a quick introduction to Django, a Python framework which is supported by the Django Software Foundation.

The Django project (https://www.djangoproject.com/) aims to encourage “rapid development and clean, pragmatic design.”  Our initial concentration will be on the object-relational mapper as we reflect on our earlier consideration of modelling and how Django can help us with such modelling.

Django is designed for building web apps and provides an automatic admin interface and a good templating system.  However, the object-relational mapper can be used completely independently of these features, and this is where we will start.

POTS

In this newsletter, our first foray into modelling was to consider a cupboard full of kitchen objects (see Issues #4, #5, and #6-7), and we will now revisit them with a focus on how we would model them with Django and whether there would be any changes worth making or any changes forced on us.

For convenience, I have copied our POTS table below.

Table POTS

Column name Description
TYPE The type of pot
SIZE The size of pot
AVAILABLE Is the pot available for use?
DIRTY Is the pot dirty? (Hmmm.  If so, is it AVAILABLE too?)

Let’s transfer this to a Django model.  If you are not familiar with Django, you may find it worthwhile to look around the Getting Started with Django page (https://www.djangoproject.com/start/), or you can just read along here anyway – it shouldn’t be too hard to understand.  Django is built in Python, so if you are not familiar with Python, you may want to read a Python tutorial, or again, just keep reading – it shouldn’t be too difficult.

If you want to work through these examples yourself, you will need to have Python and Django installed.  See the notes below for more information.

Django POTS

We will call our first Django class POT (singular), and add the attributes we need as fields in the class.

1
2
3
4
5
6
7
8
from django.db import models
 
class POT(models.Model):
 
    TYPE = models.CharField(max_length=20)
    SIZE = models.CharField(max_length=20)
    AVAILABLE = models.BooleanField()
    DIRTY = models.BooleanField()

 

Very simple.  The django-admin tool then uses this definition and does all the work for us to create a table in the backend database.

But what is the actual table like?  Some familiarity with Django is required to know this before we create the table, but we can also find it out from the database.  And when we do so, we will find that the table varies a little depending on the type of backend database.  Django supports several different backend databases, but in this series we will only consider Oracle, PostgreSQL and SQLite.  For what it is worth, I prefer Oracle because I am most familiar with it, while the Django developers recommend PostgreSQL because they’re PostgreSQL fans.

Next week

So what is the actual table like when we use django-admin to create it?  God willing, we will look at this next week.

If you are already familiar with Django, that’s all for this week.  However, if you are not very familiar with Django, the following sections can guide you through what you need to get your own working installation of Django.

Reading

If you are not familiar with Django, you may wish to do a little reading about it first.  Here are some links to information that might be helpful.

Python software and documentation are available from https://www.python.org/.

Django software, introductions, tutorials and detailed documentation are available from https://djangoproject.com/.  Wikipedia also has a page which provides a quick summary: https://en.wikipedia.org/wiki/Django_(web_framework).

Bits and pieces

This section is for anyone who is interested in creating a Django installation with the bits and pieces that are needed.  Django is a Python framework, so you need Python installed first.

Hint: If you have a 64-bit Windows operating system, you can choose to install either 32- or 64-bit software, but it is generally easiest to install 64-bit versions.  If you choose to install the 32-bit versions, make sure that Python and your database client(s) are 32-bit.  This will add some complications if you are needing to build cx_Oracle.

Django requires Python database drivers to be installed, depending on the databases to be accessed.

  • Oracle requires cx_Oracle
  • PostgreSQL requires psycopg2
  • SQLite requires sqlite3 (shipped with Python), but will use pysqlite2 by preference if it is installed.

Software Versions

As with all software that is still being developed, the version numbers are a moving target and so it is best to look at the requirements for each component we need, to make sure all the components will work together: operating system, database, Python, Python database drivers and Django.  So for example, today (29 December 2016), running on 64-bit Windows 10, the most recent versions of components that should work together are:

  • Oracle XE (11g Release 2) and/or PostgreSQL (9.6.1)
  • Python 3.5 (note that Python 3.6 will probably not be supported until Django 1.11).  Make sure you install pip and then you can easily install the extra Python components (using “pip install <package>”):
    • Django 1.10.4
    • cx_Oracle 5.2.1 and/or psycopg2 2.6.2

Software downloads

Databases

Python https://www.python.org

Django and the database drivers are most easily installed using pip.  In Windows, use the command prompt run as Administrator.  Change to the Scripts directory under the location where you have Python installed and issue the following commands:

pip.exe install django
pip.exe install cx_Oracle
pip.exe install psycopg2

Using Django

After you have installed all of these components, you should be ready to use Django.  If you have had any difficulties, go to the Django “Quick Install Guide” https://docs.djangoproject.com/en/1.10/intro/install/.  This should help you track down solutions to your problems.  If you are working with a database you have not used before, you may need to refer to https://docs.djangoproject.com/en/1.10/topics/install/#database-installation.

Once the components are working, try following the Django tutorial (https://docs.djangoproject.com/en/1.10/intro/tutorial01/) .

After that, you can try adding the POT class to your Django models file.  If that all works, it’s quite an achievement.  Well done.