Management Commands
Smarter subclasses Django’s management command framework in order to provide more consistent console output and app logging. SmarterCommand is the base class for all management commands in Smarter, and serves as a drop-in replacement for Django’s base command class.
For more details on the original Django management command interface, see the
django.core.management.base.BaseCommand documentation.
Example
from smarter.lib.django.management.base import SmarterCommand
class Command(SmarterCommand):
help = "My custom management command."
def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS("Hello from SmarterCommand!"))
- class smarter.lib.django.management.base.SmarterCommand(*args, **kwargs)[source]
Bases:
BaseCommandBase class for custom Django management commands in the Smarter framework.
This class extends Django’s
BaseCommandto provide a standardized interface and additional helper methods for writing robust, user-friendly management commands. It is intended to be subclassed by all custom management commands in the Smarter project.Features:
Standardized output formatting for command start, success, and failure.
Optional display of Django settings at the start of command execution.
Enhanced error handling with clear messaging and exit codes.
Extensible argument parsing via
create_parser.
Parameters:
All parameters accepted by Django’s
BaseCommandare supported.Command-Line Options:
--settings_output(bool): If specified, outputs Django settings at the beginning of the command’s console output.
Methods:
handle_begin(): Prints a formatted header indicating the start of the command.handle_completed_success(msg: Optional[str] = None): Prints a formatted success message. Ifmsgis provided, it is displayed; otherwise, a default message is shown.handle_completed_failure(err: Optional[Exception] = None, msg: Optional[str] = None): Prints a formatted error message. Iferris provided, the error details are included and the process exits with code 1.create_parser(prog_name, subcommand, **kwargs): Extends the default argument parser to include the--settings_outputoption.handle(*args, **options): Abstract method to be implemented by subclasses. Contains the main logic for the command.
Example Usage:
from smarter.smarter.lib.django.management.base import SmarterCommand class MyCommand(SmarterCommand): help = "My custom command." def handle(self, *args, **options): self.handle_begin() # Command logic here self.handle_completed_success("MyCommand finished successfully.")
Notes:
Subclasses must implement the
handlemethod.Use the provided helper methods to ensure consistent output and error handling.
Warning:
If
handle_completed_failureis called with an exception, the process will exit with code 1.Do not override
__init__unless necessary; always callsuper().__init__.