When developing application using Django framework, we extensively use various Django commands. Few common Django commands we use regularly are:

  • python manage.py runserver
  • python manage.py makemigrations
  • python manage.py migrate

These commands are built in and lies within Django itself.

We can also write custom Django admin command. Today I will show an example of wrinting custom Django admin command.

alt example of django custom command

These custom admin commands can be invoked using manage.py COMMAND_NAME.

To add custom Django admin command, firstly we need to add a directory management/commands to any of the apps folder.

Suppose, we need to write a custom Django admin command to insert some data from a CSV file to existing model.

Let’s name the command as insert_upazila_office_reports.py.

We have following CSV files:

  • acland_offices_rank.csv
  • uno_offices_rank.csv

After placing the files in respective directories, directory structure may look like this:

├── manage.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── APP1
│   ├── admin.py
│   ├── apps.py
│   ├── fixtures
│   │   ├── fixture_1.json
│   │   └── fixture_2.json
│   ├── __init__.py
│   ├── management
│   │   └── commands
│   │       ├── insert_upazila_office_reports.py
│   │       ├── acland_offices_rank.csv
│   │       └── uno_offices_rank.csv
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── templates
│   │   ├── base_generic.html
│   │   └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── requirements.txt

The name of the CSV file will be passed as argument to the Django command.

The desired functionality of our command is to insert data from passed CSV files to existing model.

This command will insert data from CSV file to UNOOfficeReport model assuming the CSV file name is passed. Additionally, it will insert data to ACLandOfficeReport model if --acland optional argument is passed.

Let’s create the insert_upazila_office_reports.py.

import csv
import os
from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
from reports.models import UNOOfficeReport, ACLandOfficeReport

class Command(BaseCommand):
    help = "Insert Upazila office reports from a CSV file. " \
           "CSV file name(s) should be passed. " \
           "If no optional argument (e.g.: --acland) is passed, " \
           "this command will insert UNO office reports."

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.model_name = UNOOfficeReport

    def insert_upazila_report_to_db(self, data):
        except Exception as e:
            raise CommandError("Error in inserting {}: {}".format(
                self.model_name, str(e)))

    def get_current_app_path(self):
        return apps.get_app_config('reports').path

    def get_csv_file(self, filename):
        app_path = self.get_current_app_path()
        file_path = os.path.join(app_path, "management",
                                 "commands", filename)
        return file_path

    def add_arguments(self, parser):
                            help="Inserts Upazila Office reports from CSV file")
        # Named (optional) arguments
            help='Insert AC land office reports rather than UNO office',

    def handle(self, *args, **options):
        if options['acland']:
            self.model_name = ACLandOfficeReport

        for filename in options['filenames']:
            file_path = self.get_csv_file(filename)
                with open(file_path) as csv_file:
                    csv_reader = csv.reader(csv_file, delimiter=',')
                    for row in csv_reader:
                        if row != "":
                            words = [word.strip() for word in row]
                            upazila_name = words[0]
                            office_name = words[1]
                            rank = int(words[2])
                            data = {}
                            data["upazila"] = upazila_name
                            data["office_name"] = office_name
                            data["rank"] = rank
                                self.style.SUCCESS('{}_{}: {}'.format(
                                        upazila_name, office_name,                                        

            except FileNotFoundError:
                raise CommandError("File {} does not exist".format(

We can invoke the command like:

python manage.py insert_upazila_office_reports uno_offices_rank.csv


python manage.py insert_upazila_office_reports --acland acland_offices_rank.csv

Important fact about writing custom Django admin command

  • The command should be a Python class extending BaseCommand class from django.core.management.base
  • The command file should be placed in management/commands directory.
  • If you implement __init__ in your subclass of BaseCommand, you must call BaseCommand’s __init__:

    class Command(BaseCommand):
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          # ...

Figure to get details of our custom Django admin command:

alt django custom command help



Cite This Work
APA Style
Shovon, A. R. (2019, September 26). Writing Django Custom Management Command. Ahmedur Rahman Shovon. Retrieved March 29, 2024, from https://arshovon.com/blog/django-custom-command/
MLA Style
Shovon, Ahmedur Rahman. “Writing Django Custom Management Command.” Ahmedur Rahman Shovon, 26 Sep. 2019. Web. 29 Mar. 2024. https://arshovon.com/blog/django-custom-command/.
BibTeX entry
@misc{ shovon_2019,
    author = "Shovon, Ahmedur Rahman",
    title = "Writing Django Custom Management Command",
    year = "2019",
    url = "https://arshovon.com/blog/django-custom-command/",
    note = "[Online; accessed 29-March-2024; URL: https://arshovon.com/blog/django-custom-command/]"
Related Contents in this Website