Django tips: Automating Database Tasks

Learn how to access your Django models from a standalone Python script.

Django is not a minimal framework, it feels you can’t escape its architecture, and not everything needs to be inside view, inside an app. There’s a way to write standalone scripts, I usually use them to populate sample data in a development environment, so a new developer can clone the repository, run the populate script in order to have some basic data on his/her local server, in many cases a project will not work with an empty database, when configurations are stored in a model.

Recently I had to perform an automated task on a project, the idea was to update the prices of all products under a category, it could be possible to edit them manually in Django admin, but my client has over 200 products, doing it manually would be a tedious task, subject to human mistakes. In case of large scale projects, an automation script is really the only solution here.

Create a new Python script in the project root, in the same directory of

import django
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'MyProject.settings'

Run it: python, expect empty output. You can use cron to run it periodically.

From now on you can do anything you would do in a Django view, like create, retrieve, update and delete objects, just import your models:

from your_app.models import MyModel

# create
row = MyModel(name='T-Shirt Batman', price=120)

Let’s say we want to update the prices of all our products that costs $120 to $150, but only for ‘Batman’ products:

rows = MyModel.objects.filter(price=120)
for row in rows:
        if 'batman' in
            row.price = 150
            print("Product updated: " + str(

Just test your script in a development environment before running it in production and you’re ready to go, unless you like to live dangerously.


Written on September 22, 2016