a blog about Django & Web Development

How to Generate Fake Data for your Django Project with Faker

Learn how to write scripts that will generate fake data for your project.

Development is more efficient when you have data to work with. It’s hard to visualise what some of your pages will look like without data to fill them. For example, you don’t want to discover issues with your page layouts until after you go Live.

Creating test data will help mimic conditions of a Live site, which will help you find bugs early.

The problem is, creating lots of data manually is time consuming. In some cases, you may need hundreds or thousands of rows in your database to really understand how well your application performs.

Thankfully, there are solutions that will help you create data quickly.

Option 1: Fixtures

One of these methods is to create a fixture (JSON file). Django provides management commands (docs) such as dumpdata which exports data from an existing database to a JSON file, and loaddata which transfers data from JSON to the database.

This is useful if you already have a database to export data from, but it’s less useful when you’re starting from scratch.

Option 2: Write a script

Another solution is to write a script that will create objects in the database for you.

To automatically generate data for our fields, we will install a package called Faker (docs), a Python package that will generate all kinds of fake data.

To Do List Example

In this tutorial, we are going to use a To Do list app as an example.

We will write a script that will create 30 tasks for our To Do list app.

The code

You can find the before and after code for this tutorial on GitHub.

Step 1: Install Faker

Make sure your project has an active virtual environment.

Install Faker by running the following command in your terminal:

pip install faker

If you’re using requirements.in, then add faker to the file.

Step 2: Create a script

Create a script template

In the root directory (same directory as manage.py, create a file called create_tasks.py.

We are going to start with a template for a Python script. Unlike regular Python scripts, you will be able to import Django modules.

# create_tasks.py

def main():
    tasks = Task.objects.all()
    print(f"There are {tasks.count()} tasks in the database")


if __name__ == "__main__":
    import os

    from django.core.wsgi import get_wsgi_application

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
    application = get_wsgi_application()

    from todo.models import Task

    main()

Test that the script runs correctly by running:

python create_tasks.py

I get the following output:

There are 3 tasks in the database

Step 2: Create some fake data

This is my script.

# create_tasks.py


def main():
    fake: Faker = Faker()

    for i in range(30):
        task = Task.objects.create(
            name=fake.paragraph(nb_sentences=1),
            status=random.choice(Task.StatusChoice.choices)[0],
        )
        print(f"Created todo. Name: {task.name}  Status: {task.status}")

    task_count = Task.objects.count()

    print(f"There are {task_count} todos in the database")


if __name__ == "__main__":
    import os

    from django.core.wsgi import get_wsgi_application

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
    application = get_wsgi_application()

    import random

    from faker import Faker
    from todo.models import Task

    main()

On this line: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings"), you will need to substitute mysite with the name of your project (name of the folder than contains settings.py.

This script will create 30 tasks in the database.

It begins by creating an instance of Faker. The instance will have access to Faker’s methods such as address and paragraph.

It then creates a Task in the database. For the name, it uses the fake.paragraph method to generate a name. For the status, we want to pick one randomly from the choices defined on the model (GitHub).

Run the script

To run the script, run the following command in your terminal:

python manage.py create_tasks.py

You should get an output similar to this:

...
...
Created todo. Name: Tend term citizen center.  Status: To Do
Created todo. Name: Strong nor radio official.  Status: Done
Created todo. Name: Theory which authority with bring campaign.  Status: Doing
Created todo. Name: Each expect certainly break whom cup.  Status: Doing
Created todo. Name: Effort pull send condition pull war his.  Status: Done
There are 33 todos in the database

Check Admin

Go to the admin area to check that your task objects have been created correctly:

A screenshot of Django admin showing the tasks created by the script.

Related Posts

To Do List Part 4

To Do List – Part 4: Adding a form

Step 1: Create a form class Django takes an object orientated approach to forms. The form classes can give you functionality including: Rendering entire forms