Django Scaffolding

We all love Django — a simple but powerful framework. However, Ruby on Rails has at least one (and few others I’m sure!) advantage: scaffolding. It allows developers to create the models, views, and templates in a single operation. It’s an interesting solution — you don’t have to manually create all the things that usually take a lot of time. So why don’t we have this feature in Django? Well… now we do!

Our latest django-common 0.3 comes with a scaffolding feature!


Here is what django-common scaffold can do:
create app
– create models
– create views
– create templates
– create forms
– create urls
– create tests

Scaffold creates the app (similar to startapp that ships with Django), models with fields, CRUD views with ajax forms, prepares templates, fills urls, and creates CRUD tests with only one command!


Use pip installer:

pip install django-common-helpers

Or just download/clone directly from github:

Then add it (django_common) to INSTALLED_APPS and set up SCAFFOLD_APPS_DIR in settings.

Default is set to main app directory. However if you use django_base_project you must set up this to SCAFFOLD_APPS_DIR = ‘apps/’.

Run scaffold, run!

To run scaffold type:

python scaffold APPNAME --model MODELNAME [fields]

APPNAME is the name of the new application. If application already doesn’t exist it will be created.

MODELNAME is the name of the model. Just enter model name that you want to create (for example: Blog, Topic, Post etc). It must be alphanumerical. Only one model per run is allowed!

[fields] – list of the model fields.

Field Types

Available fields:

char - CharField
text - TextField
int - IntegerFIeld
decimal -DecimalField
datetime - DateTimeField
foreign - ForeignKey

All fields requires name that is provided after “:“ sign, for example:

char:title text:body int:posts datetime:create_date

Two fields, “foreign“ and “decimal“ require additional parameters:

– “foreign” as third argument takes foreignkey model, for example:

foreign:blog:Blog, foreign:post:Post, foreign:added_by:User

Notice: All foreign key models must already exist in project. User and Group models are imported automatically.

– decimal field requires two more arguments “max_digits“ and “decimal_places“, for example:


Notice: To all models scaffold automatically adds two fields: update_date and create_date.


Scaffold templates use two blocks extending from base.html:

{% extends "base.html" %}
{% block page-title %} {% endblock %}
{% block conent %} {% endblock %}

So be sure you have your base.html set up accordingly.

Example Usage

Let’s create a very simple “forum“ app. We need “Forum“, “Topic“ and “Post“ models.

Forum model
Forum model needs just one field “name“:

python scaffold forum --model Forum char:name

Topic model
Topics are created by site users so we need: created_by, title and Forum foreign key (update_date and create_date are always added to models):

python scaffold forum --model Topic foreign:created_by:User char:title foreign:forum:Forum

Post model
Last one are Posts. Posts are related to Topics. Here we need: title, body, created_by and foreign key to Topic:

python scaffold forum --model Post char:title text:body foreign:created_by:User foreign:topic:Topic

All data should be in place!

Now you must add forum app to INSTALLED_APPS and include app in file by adding into urlpatterns:

urlpatterns = patterns('',
    (r'^', include('forum.urls')),

Now syncdb new app and you are ready to go:

python syncdb

Run your server:

python runserver

And go to forum main page:


All the structure is in place. Now you can personalize models, templates and urls.

At the end you can test new app by running test:

python test forum

Creating test database for alias ‘default’…
Ran 7 tests in 0.884s


Happy scaffolding!

Django Scaffolding

Leave a Reply

Your email address will not be published. Required fields are marked *