Django: Getting Started Transcripts
Chapter: Custom Django Commands
Lecture: Structure of a management command
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I've mentioned before that the Django core team has a practice of building the library in applicable fashion,
0:06
allowing you the programmer to use the same mechanisms as the module developers.
0:10
Django management commands are no different by placing a Python script in the right location and inheriting from a class.
0:18
You can write your own management command in only a few lines of code. Management commands are contained within an app
0:25
and must be inside of a nested directory called management. Then commands, this always feels to me like they had bigger plans for this,
0:33
you end up with a two level directory structure on only ever using the bottom folder, but as strange as it may be, this is how it works.
0:41
By putting your script in this directory, it will automatically get picked up.
0:46
It will show up in the listing of commands when you run manage.py without any arguments.
0:51
The name of the command will be whatever you named the script file. Inside of your script file,
0:56
you need to create a class that inherits from base command There are a couple of key methods you need to define to get things going
1:04
and the system uses argparse to pass arguments into your command. Let's go write some code.