This is the first article posted. I decided to start wanting to leave what I learned in a memorable way.
Recently, I am studying with the motto of mastering django deeply. This article is a footstep in learning about django's custom commands.
It is a mechanism that can execute python scripts with CUI,
・ Various functions of django (db ORM etc.) can be used ・ Easy command management
There are advantages such as. The latter is
-List available commands-
python manage.py --help
[shop]
customcommand
cusyomcommand2
...
-Show custom command description-
python manage.py customcommand --help
usage: manage.py customcommand2 [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback]
[--no-color] [--force-color] [--skip-checks]
Function to store data
This is possible.
(1) Create an application (this time created with the name script)
python manage.py startapp script
(2) Create a hierarchical structure of management / commands
script/
├── management/
├── commands/
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py
(3) Put the script file under commands Here, create it with the name customcommand.py
When I read the comment of the baseBaseCommand class, it says, "If you don't want to change the command execution flow, you can use a child class named Command that inherits the BaseCommand class." I think that the following configuration is okay.
customcommand.py
class Command(BaseCommand):
#Describe the content you want to process in the script in the overridden handle function
def handle(self, *args, **options):
pass
This is a story about how the custom command of the main subject is called and what kind of function is used for processing.
The processing flow is as follows.
Execute python manage.py customcommand
Call the Command class
run_from_argv method
create_parser method
add_arguments method
parse_args method
execute method
handle method
I got the image that a function called run_from_argv shows the overall processing flow and performs detailed processing in other functions. Therefore, the concept of detailed processing after 4 is described below.
Arguments are in the form of [command line argument [0], command line argument [1]] -> In this example, ['manage.py','customcommand']
First, cut out the file name of manage.py. -> If manage.py is described with the full path, only manage.py will be retrieved here.
Next, create a Parser object. The Parser object has the following attributes. -prog: "manage.py customcommand" (instructions separated by spaces) -description: help statement (obtained by self.help) -formatter_class: (predetermined Django help option such as --version) Such
Add the argument you want to use in the process to the parser object. For example
parser.add_argument('parameters', nargs='+', type=int)
Then you can force one or more arguments of type int.
-> ``` Python manage.py customcommand 3 5``` etc.
### 6. parse_args method
Here, check the format of the instruction group actually entered.
-If Django's help option such as --version comes, end the process here
-If the input format is different, the expected argument (contents of parser) is output as standard.
### 7,8. execute method, handle method
It is passed to the execute method and passed to the handle method only when the format check is passed in the process of 6.
* If an argument is added in the process of 5, it can be referred from the options argument of the handle method.
# 5. Customize custom commands
### Add command help statement
```python
help = 'Function to'
OK if you define the help argument
A brief summary of how to use ArgumentParser It was explained in detail in this article.
I searched a lot, but I got the impression that it would be okay if I put down the handle function, add_arguments function, and the addition of help. As for the concept of parse object, it seems to be widely used for handling variable arguments, so I learned it.
Recommended Posts