This is the code repository of the following tutorial: https://arshovon.com/blog/django-custom-command/
.
├── mysite
│ ├── manage.py
│ ├── mysite
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ └── polls
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── management
│ │ └── commands
│ │ ├── command_utils.py
│ │ ├── insert_dummy_questions.py
│ │ └── questions.csv
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── templates
│ │ └── polls
│ │ ├── detail.html
│ │ ├── index.html
│ │ └── results.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
└── requirements.txt
- Python version: 3.6+
- Create virtual environment:
python3 -m venv venv
- Activate virtual environment:
source venv/bin/activate
- Install required packages:
pip install -r requirements.txt
- Create migration:
python manage.py makemigrations
- Migrate migrations:
python manage.py migrate
- Create super user:
python manage.py createsuperuser
- Run the project from the root directory of Django project:
python manage.py runserver
-
Run Django custom command
help
context:python manage.py insert_dummy_questions --help
-
Run Django custom command:
python manage.py insert_dummy_questions questions.csv
-
After running Django custom command the CSV file data is inserted into database:
- I have used Django Docs Example Polls Application to demonstrate Django Custom Command.
- Utility methods of custom command files are kept in a separate file in
mysite/polls/management/commands/command_utils.py
. Then in custom command file I have imported required methods like:from .command_utils import get_csv_file