liveBook · Manning

Argparse namespace

As explained in Django in Action

An object whose attributes contain values parsed from command-line arguments.

An argparse namespace is the dictionary-like object available through a Django management command’s options parameter. It holds positional arguments and flags passed on the command line. After add_arguments() defines --last_name, for example, options["last_name"] contains the supplied value, or None when the flag was omitted. section 9.3

Filtering musicians

Running python manage.py musicians --last_name Loblaw places the flag’s value in options["last_name"]. The command can then pass that value into a database filter; without the flag, the value is None. section 9.3

How Django in Action covers it

How deep it goes
Explains dictionary-like storage and flag lookup, but stops short of argparse implementation internals.
Examples
Demonstrates last-name, first-name, and birth-date flags with musician-listing commands.
What it assumes
Assumes familiarity with management-command methods and command-line flags, while showing how parsed values become accessible.
What you can do after
Operate/configure Django management commands by reading supplied flags and applying their values conditionally.
How it connects
Connects command-line arguments, argparse, Django management commands, and database queries.

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.