$ show django-commons/django-click
django-click is a library that bridges the Click CLI framework with Django's management command system. It allows developers to write Django management commands using Click's decorator-based API instead of the traditional Django BaseCommand class. The library provides a drop-in replacement for Click's `command` and `group` decorators that automatically register the decorated function as a Django management command, handling all the integration details such as argument parsing, help output, verbosity, settings, and color support. The architecture consists of adapter classes (`DjangoCommandMixin`, `CommandAdapter`, `GroupAdapter`) that inherit from Click's `Command` and `Group` classes and override key methods (`run_from_argv`, `create_parser`, `execute`, `print_help`) to conform to Django's command execution protocol. A `BaseRegistrator` class handles the registration process: it extracts the command name from the module name, applies Click decorators with common Django options (verbosity, settings, pythonpath, traceback, color), and sets the module-level `Command` attribute to a lambda that returns the Click command instance. This enables seamless integration with both `manage.py` invocation and `call_command()` usage. The library also provides a `pass_verbosity` decorator for accessing the Django verbosity level within Click commands, and a `ModelInstance` custom parameter type that allows commands to accept Django model instances as arguments with support for custom lookups. The code is well-tested with comprehensive test coverage for CLI invocation, `call_command` integration, error handling, color support, and edge cases like abort handling and stdout/stderr capture.
The LLM identified 2 files worth highlighting for reuse:
Core adapter module that bridges Click and Django management commands. Contains DjangoCommandMixin, CommandAdapter, GroupAdapter, BaseRegistrator, CommandRegistrator, GroupRegistrator, and pass_verbosity. This component encapsulates the entire integration pattern: it overrides Click's Command methods to conform to Django's command execution interface, handles common Django options (verbosity, settings, pythonpath, traceback, color), and supports both CLI invocation and call_command() usage. It is self-contained (only imports click, os, sys, contextlib, functools, and django) and could be extracted to integrate Click with other frameworks that have similar command interfaces.
Provides ModelInstance, a custom Click parameter type that resolves Django model instances from command-line arguments. It accepts a model class or QuerySet and an optional lookup field (default 'pk'), and converts the string value to a model instance using the specified lookup. It handles the ObjectDoesNotExist exception and provides a clear error message. This component is self-contained (imports only click and django.core.exceptions) and can be reused in any Click-based Django command to accept model instances as parameters.