How it works...

Django management commands are scripts with Command classes deriving from BaseCommand and overwriting the add_arguments() and handle() methods. The help attribute defines the help text for the management command. It can be seen when you type the following in the command line:

(env)$ python manage.py help import_music_from_csv

Django management commands use the built-in argparse module to parse the passed arguments. The add_arguments() method defines what positional or named arguments should be passed to the management command. In our case, we will add a positional file_path argument of the Unicode type. By having the nargs variable set to the 1 attribute, we allow only one value.

To learn about the other arguments that you can define and how to do this, refer to the official argparse documentation at https://docs.python.org/3/library/argparse.html#adding-arguments.

At the beginning of the handle() method, the verbosity argument is checked. Verbosity defines how much Terminal output the command should provide from 0, not giving any logging, to 3, providing extensive logging. You can pass this named argument to the command as follows:

(env)$ python manage.py import_music_from_csv data/music.csv --verbosity=0

We also expect the filename as the first positional argument. options["file_path"] returns a list of the values with the length defined in nargs. In our case, nargs equals one; therefore, options["file_path"] will be equal to a list of one element.

It's a good practice to split the logics of your management command into multiple smaller methods, for example, like we use in this script with prepare(), main(), and finalize():

  • The prepare() method sets import counters to zero. It could also be used for any other setup that is necessary for the script.
  • In the main() method, we execute the main logic of the management command. At first, we open the given file for reading and pass its pointer to csv.DictReader. The first line in the file is assumed to contain headings for each of the columns. DictReader uses them as keys for the dictionaries for each row. When we iterate through the rows, we pass the dictionaries to the model form and try to validate it. If validation passes, a song is saved and imported_counter is incremented. If validation fails, because of too long values, missing required values, wrong types, or other validation errors, skipped_counter is incremented. If verbosity is equal or greater than NORMAL (which is number 1), each imported or skipped song is also printed out together with possible validation errors.
  • The finalize() method prints out how many songs were imported and how many were skipped because of validation errors.

If you want to debug the errors of a management command while developing it, pass the --traceback parameter to it. When an error occurs, you will see the full stack trace of the problem.

Assuming we invoked the command twice with --verbosity=1 or higher, the output we could expect might be as follows:

As you can see, when a song is being imported a second time, it doesn't pass the unique_together constraint and therefore is skipped.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.246