Understanding Django’s custom management commands

Mohammad Asim Ayub
CodeX
Published in
4 min readApr 18, 2021

--

Photo by Nathana Rebouças on Unsplash

Introduction

Django is a free open-source python back-end web framework that removes the tedium and comes packed with features that are required by almost every web application. It provides lots of management commands out of the box that allows users to accomplish important tasks. Of these, almost everyone has used the makemigrations , migrate and createsuperuser commands.

However, Django also provides us with the ability to create our own management commands for any application that we want. Management commands are really helpful for running periodic tasks in your application and are run mostly either after each deployment or as corn jobs.

An example of creating a management command would be its use for data migration from one database to another. Let’s assume you have an existing application running in production and new features have been added to this application that involves changes to the data structure. To avoid any problems with the live data, you instead create a new database, create a custom management command that is executed when the application is upgraded. This command would migrate the data from the old database to the new one and in the process modifying it to fit the new data structure. Another example of this could be running periodic data cleanup tasks on your database.

--

--

Mohammad Asim Ayub
CodeX

A DevOps engineer by profession. I love learning about new technologies and sharing my learning with others. Visit me @ www.asimayub.com