Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Rails Support
      • Working with Bundler
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Language Support
  • Python
  • Working with Django
  • Scheduling Custom Django Management Commands

Scheduling Custom Django Management Commands

English — 日本語に切り替える

Last updated October 18, 2021

Table of Contents

  • Create a custom management command
  • Setup Heroku Scheduler

Running scheduled tasks that rely on data or code in your Django application is simple with custom django-admin commands and Heroku Scheduler.

Create a custom management command

Create the Python file

Custom Django management commands are nested within a Django project’s apps. For this tutorial, if the Django project contains more than one app, decide which app the custom management command will most closely relate to, and use that app to follow the steps below.

A Django project contains one or more apps. In this tutorial, you’ll be working within one of the apps and not the overall Django project.

The Python file that contains the code for the custom command is nested inside several directories:

  1. At the root level of the Django app, create a directory called management
  2. Within management create a commands directory
  3. Within commands create a file named something similar to print_book_titles.py. Stylistically, the name you choose for this file should reference the outcome of the task when it runs.

At this stage, the app’s file tree will look similar to this:

some_app/
    __init__.py
    models.py
    management/
        commands/
            print_book_titles.py
    tests.py
    views.py

Django automatically maps the name of the command file to the command that is run in the CLI (python manage.py print_book_titles)

Code the command’s logic

The following code snippet is for a short custom management command. View the Django documentation on Writing custom django-admin commands for guidance on more complex needs.

from django.core.management.base import BaseCommand, CommandError
from some_app.models import Book

class Command(BaseCommand):
    help = 'Prints all book titles in the database'

    def handle(self):
        try:
            books = Book.objects.all()
            for book in books:
                 self.stdout.write(self.style.SUCCESS(book.title))
        except FieldDoesNotExist:
            self.stdout.write(self.style.ERROR('Field "title" does not exist.'))
            return

        self.stdout.write(self.style.SUCCESS('Successfully printed all Book titles'))
        return

Include a return statement at the end of each logical flow of the handle() function so Heroku Scheduler knows when it can shut down. This ensures you are only charged for dyno time that is needed to run the job.

Verify the command works as expected

Ensure the command works by running it in a one-off dyno:

  1. From the CLI, run heroku run bash -a your-app-name
  2. Navigate to the directory containing manage.py
  3. Run python manage.py <your_custom_command>

Setup Heroku Scheduler

Provision the Add-on

If the Heroku Scheduler Add-On is not already provisioned on the Heroku app, follow the steps in Heroku Scheduler - Installing the Add-On now.

Schedule the job

Open the Heroku Scheduler tool by following the steps in Heroku Scheduler - Scheduling Jobs. When prompted, enter python manage.py <your_custom_command> as the command to run.

If the manage.py file is not at the root level of the Heroku app’s git repo, the command that is run within Heroku Scheduler will need to first navigate to the directory that contains the manage.py file. The command would look something like cd my-project && python manage.py <your-command-name>.

Keep reading

  • Working with Django

Feedback

Log in to submit feedback.

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences