San Francisco based Software Engineer at Sony Network Entertainment. He primarily acts in a generalist fashion, working on everything from tools development, to engine design, to game programming, but his real passion is software architecture and optimization. He’s also completed Deadly Premonition. You can find him online at
Posts by Don Olmstead
  1. AltDevPanel on Optimization Video ( Counting comments... )
  2. AltDevPanel on Optimization ( Counting comments... )
  3. Gathering and Analyzing Data with Django: Part 3 ( Counting comments... )
  4. Announcing AltDevPanel ( Counting comments... )
  5. Abusing C++ with Expression Templates ( Counting comments... )
  6. #include <best_practices> ( Counting comments... )
  7. Gathering and Analyzing Data with Django: Part 2 ( Counting comments... )
  8. Gathering and Analyzing Data with Django: Part 1 ( Counting comments... )
  9. Defining a SIMD Interface: Redux ( Counting comments... )
  10. Defining a SIMD Interface ( Counting comments... )
  11. Creating Camera Based Interactions ( Counting comments... )
Technology/ Code /

Playtesting is a great way to tune gameplay. It can provide insightful feedback which can be applied to provide a superior experience. However there may be some valuable information that is not articulated through interviews, because its not something the player is conscious of. By recording their encounters and then analyzing them new observations can be made which can further enhance the game.

In this series of articles we'll be developing a web site that gathers gameplay data, processes it and then visualizes it. It is structured as a primer on Django, and explores many of its features. This knowledge can then be leveraged when analyzing your own game.

The Problem

Suppose we’re working on a RPG. One of the things we want to keep track of is a player’s progress through the various quests in the game. The data we’d like to glean is how many players encountered the quest, how many finished it, and how long it took to finish it. This information will give us some insight as to how well the quest is received.

Introducing Django

Django is an open-source web framework written in Python. The benefit of using a web framework is its handling of the more mundane aspects of web development, such as communicating with a database. This frees the developer to focus on the functionality of the site, speeding up its development. As you’ll see over the course of the series there isn’t a whole lot of code to get the site up and running.

One of the great things about Django is its documentation. There is plenty of functionality that is not touched upon within this article so exploring the documentation is the best way to see all the things that are possible when using the framework. To get the framework installed the best information is contained on their site.

Building the Site

Width Django installed we can begin creating the web site. In Django terminology a web site is made up of one or more projects. A project is a collection of applications, which will be introduced shortly, all sharing the same database and settings. To start a project the script is used.

django-admin startproject gameanalytics

This call creates a gameanalytics directory and three files of interest. The fourth file,, is a Python convention for marking directories as a Python package and will be found in multiple places in the application. Its normally an empty file so we’ll be ignoring it for the duration.

  • - A utility we’ll be using to perform various tasks in the web application.
  • - A script containing various configuration options, such as the database
  • - The URL mappings for the application.

The next step is to create an application within the project. The way applications are used within a project is similar to an Entity System. Each application provides a single set of self-contained functions, and the combination of them defines the behavior of the project.

python startapp game

This command creates the game directory and three files of note.

  • - Defines the data being stored.
  • - Contains unit tests.
  • - Defines functions that will be called during URL requests.

Architecturally Django follow the Model-View-Controller pattern as evidenced through its naming scheme. Django refers to its variant as Model-Template-View. In terms of mapping between the two the View can be thought of as the Controller, which contains the business logic, the Template corresponds to the View, which handles the presentation, and the Model is just that, the data.

Modeling the Game Data

We’ll begin development by opening and defining our data.

from django.db import models
class Player(models.Model):
	name = models.CharField(max_length=100)
class Quest(models.Model):
	name = models.CharField(max_length=100)
	code = models.CharField(max_length=100, unique=True)
class QuestState(models.Model):
		(DECLINED_STATUS, 'Declined'),
		(ACCEPTED_STATUS, 'Accepted'),
		(COMPLETED_STATUS, 'Completed')
	player = models.ForeignKey(Player)
	quest = models.ForeignKey(Quest)
	status = models.IntegerField(choices=QUEST_STATUS, null=True)
	start_time = models.IntegerField(null=True)
	end_time = models.IntegerField(null=True)

Django provides Object-Relational Mapping, which translates between the Object-Oriented data model and the underlying relational database. This functionality is contained in the Model class which our classes descend from in order to access.

The individual fields expressed in the class definition correspond to fields within the database. The full listing of potential fields can be found in the documentation. Each of them can take additional arguments which allow for more control of the underlying data. In our example we created an enumeration for the quest status, and allowed it to contain a null value.

A quick aside about fields. It is possible in Django to create custom fields. In this example the start and end time contain an integer which will represent the number of seconds of playtime. From there we’ll create a timedelta object. We could skip this step by providing a custom field that works directly with a timedelta, but as this is a more advanced feature we’ll forgo this. For those interested the documentation provides an example.

Before we can create the database schema we need to configure the database connection. This is specified in within the application root. Django supports PostgreSQL, mySQL, Oracle and SQLite out of the box. For our purposes we’ll use SQLite which requires no configuration on our end, as Python has built-in support for it. When deploying to a web server this will need to be modified accordingly.

    'default': {
        'ENGINE': 'sqlite3',
        'NAME': 'test.db3',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',

Additionally we need to register our application within the project. Page down to the bottom of and modify the INSTALLED_APPS, adding the application to the listing.


Now we can create the database using

python syncdb

During execution the script will prompt you to create a superuser. This account allows you to access the admin panel. Create an account as we’ll be accessing it later.

Within the root of the application the database file, test.db, should be present. To examine the underlying schema a tool like SQLiteBrowser can be used. This can be used to examine how our models map to the database.

Database Schema

The Admin Interface

Next we’ll enable the admin site so we can create some data. First we need to uncomment the admin component in Next we modify enabling the three lines related to the admin site.

Now we can startup the site. Django provides a built-in web server that can be used during development. It is started by, you guessed it,

python runserver

Login to the admin site by navigating to Afterwards your screen should look like this.

The Admin Panel

The Admin Panel

Here you can add additional logins, but that’s about it currently. To access our data we need to register it with the admin component. Create and modify it as follows to hook it into the admin panel.

from django.contrib import admin
from models import *

Restart the web server and refresh the web page to see our models.

Admin Panel with Models Added

Admin Panel with Models Added

Through this interface we can create, read, update and delete data. By using introspection on the models the admin panel generates forms to do all these tasks. It even manages to populate a list box with the names of our enumeration!

Adding Data in Bulk

If we have quite a bit of information to import then adding it all through the admin panel can become tedious. This is where fixtures come into play.

A fixture is simply a JSON, or YAML, file that maps to a model. Create a fixtures folder within the component and add the following to intial_data.json. Here we’ll add some quests.

		"model": "",
		"pk": 1,
			"name": "Fetch my slippers",
			"code": "FET0001"
		"model": "",
		"pk": 2,
			"name": "Make me a sammich",
			"code": "FET0002"

When running the syncdb command any data in initial_data.json will be automatically loaded. You may have caught the line “No fixtures found” the last time you ran the command. This file not being present was what it was referring to. Run the command again and you should see the data in the admin panel.

Fixtures can also be used to add some test data. Create a file called player_data.json in the fixtures directory.

		"model": "game.player",
		"pk": 1,
			"name": "The Nameless One"
		"model": "game.player",
		"pk": 2,
			"name": "Morte"

This can then be added to the database via

python loaddata player_data

Up Next

In the next article we'll add the ability to upload and parse a log file as we continue development of the analytics site.