Generating sample data

Creating a generate_sampledata function

Before you can generate data, you need to write a generate_sampledata function for every Django app that you want to generate sampledata.

For example, say you had a Django project named testproject with a directory structure like this:

|- testapp/

In the file you have INSTALLED_APPS set like this:

INSTALLED_APPS = ['testproject.testapp']

That way testproject.testapp is a valid Django app in your Django project.

In the testapp/ directory, create a file named

In the file, create a function named generate_sampledata.

Here’s an example:

def generate_sampledata(options):

In that function, you do whatever you want to do to generate data.


The options argument holds the options that the OptionParser that Django uses to parse the command line arguments. Thus it has the standard options that are passed to every Django command:

  • settings
  • pythonpath
  • verbosity
  • traceback

Most of those probably aren’t useful, but verbosity might be.

Additionally, you can use the with keyword argument to pass additional parameters which will get pulled out and added to the options dict.

For example:

def generate_sampledata(options):
    print options.get('foo')

If you do this:

$ ./ generatedata --with=foo=bar

it’ll print:


Running ./ generatedata

General use

To generate sample data, do this:

$ ./ generatedata

That will go through all your INSTALLED_APPS looking for modules named sampledata and executing the generate_sampledata method in each.

Generating data in specified apps

Say you had a bunch of apps in your Django project that have sampledata modules, but you only want to generated data in one of them. You can specify the apps you want to generate data in on the command line:

# All apps
$ ./ generatedata

# Only app1
$ ./ generatedata app1

# Only app1 and app2
$ ./ generatedata app1 app2

Passing arguments

You can also pass arguments to the generate_sampledata functions using the with keyword argument.


# Passes {'fixtures': True} to options
$ ./ generatedata --with=fixtures

# Passes {'type': 'random'} to options
$ ./ generatedata --with=type=random

# Passes {'type': 'random', 'seed': '1024'} to options.
$ ./ generatedata --with=type=random --with=seed=1024

You can have as many as you like—each will get parsed out as a separate key or key/val parameter.


Example 1: Load a fixture

This generate_sampledata function loads a fixture (ab)using the Django loaddata command:

from import loaddata

def generate_sampledata(options):
    cmd = loaddata.Command()

Run it like this:

$ ./ generatedata

Example 2: Generate data with model makers

This example has a rough model maker for the Record model. Also, it allows the user to specify how many records he/she wants to create using the count parameter:

import datetime
from someproject.someapp.models import Record

def record(**kwargs):
    rec = Record(**kwargs)
    return rec

def generate_sampledata(options):
    count = options.get('count', '10')
    count = int(count)

    now =

    # Creates count number of records, each on a new day.
    for i in range(count):
        record(created=now - datetime.timedelta(days=i),
               message='Lorem ipsum %d' % i)

Run it like this:

$ ./ generatedata
$ ./ generatedata --with=count=20