Skip to main content

My collection of things for working with Django.

Latest release 20241119: New DjangoSpecificSubCommand(CSBaseCommand.SubCommandClass) to include support for pure Django BaseCommands.

Class BaseCommand(cs.cmdutils.BaseCommand, django.core.management.base.BaseCommand)

A drop in class for django.core.management.base.BaseCommand which subclasses cs.cmdutils.BaseCommand.

This lets me write management commands more easily, particularly if there are subcommands.

This is a drop in in the sense that you still make a management command in nearly the same way:

from cs.djutils import BaseCommand

class Command(BaseCommand):

and manage.py will find it and run it as normal. But from that point on the style is as for cs.cmdutils.BaseCommand:

  • no aegparse setup
  • direct support for subcommands as methods
  • succinct option parsing, if you want command line options

A simple command looks like this:

class Command(BaseCommand):

    def main(self, argv):
        ... do stuff based on the CLI args `argv` ...

A command with subcommands looks like this:

class Command(BaseCommand):

    def cmd_this(self, argv):
        ... do the "this" subcommand ...

    def cmd_that(self, argv):
        ... do the "that" subcommand ...

If want some kind of app/client specific "overcommand" composed from other management commands you can import them and make them subcommands of the overcommand:

from .other_command import Command as OtherCommand

class Command(BaseCommand):

    # provide it as the "other" subcommand
    cmd_other = OtherCommand

Option parsing is inline in the command. self comes presupplied with a .options attribute which is an instance of cs.cmdutils.BaseCommandOptions (or some subclass).

Parsing options is simple:

class Command(BaseCommand):

    def cmd_this(self, argv):
        options = self.options
        # parsing options:
        #
        # boolean -x option, makes options.x
        #
        # --thing-limit n option taking an int
        # makes options.thing_limit
        # help text is "Thing limit."
        #
        # a --mode foo option taking a string
        # makes options.mode
        # help text is "The run mode."
        options.popopts(
            argv,
            x=None,
            thing_limit_=int,
            mode_='The run mode.',
        )
        ... now consult options.x or whatever
        ... argv is now the remaining arguments after the options

Usage summary:

Usage: base [common-options...] [options...]
  A drop in class for `django.core.management.base.BaseCommand`
  which subclasses `cs.cmdutils.BaseCommand`.
  Subcommands:
    help [common-options...] [-l] [subcommand-names...]
      Print help for subcommands.
      This outputs the full help for the named subcommands,
      or the short help for all subcommands if no names are specified.
      -l  Long help even if no subcommand-names provided.
    info [common-options...] [field-names...]
      Recite general information.
      Explicit field names may be provided to override the default listing.
    shell [common-options...]
      Run a command prompt via cmd.Cmd using this command's subcommands.

BaseCommand.SubCommandClass

BaseCommand.add_arguments(self, parser): Add the Options.COMMON_OPT_SPECS to the argparse parser. This is basicly to support the Django call_command function.

BaseCommand.handle(*, argv, **options): The Django BaseComand.handle method. This creates another instance for argv and runs it.

BaseCommand.run_from_argv(argv): Intercept django.core.management.base.BaseCommand.run_from_argv. Construct an instance of cs.djutils.DjangoBaseCommand and run it.

Class DjangoSpecificSubCommand(cs.cmdutils.SubCommand)

A subclass of cs.cmdutils.SubCOmmand with additional support for Django's BaseCommand.

DjangoSpecificSubCommand.__call__(self, argv: List[str]): Run this SubCommand with argv. This calls Django's BaseCommand.run_from_argv for pure Django commands.

DjangoSpecificSubCommand.is_pure_django_command: Whether this subcommand is a pure Django BaseCommand.

DjangoSpecificSubCommand.usage_text(self, *, cmd=None, **kw): Return the usage text for this subcommand.

Release Log

Release 20241119: New DjangoSpecificSubCommand(CSBaseCommand.SubCommandClass) to include support for pure Django BaseCommands.

Release 20241111: Rename DjangoBaseCommand to just BaseCommand so that we go from cs.djutils import BaseCommand. Less confusing.

Release 20241110: Initial PyPI release with DjangoBaseCommand, cs.cmdutils.BaseCommand subclass suppplanting django.core.management.base.BaseCommand.

Metadata

Release files for cs-djutils 20241119

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs-djutils 20241119
File Size Uploaded
cs_djutils-20241119.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cs-djutils 20241119
File Interpreter ABI Platform
cs_djutils-20241119-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / cs_djutils-20241119.tar.gz

Download URL cs_djutils-20241119.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a9ccdae7d57aa0d67b378a58ea4dd4a87afa21f9ed0b56b33f51953baf698f3b
BLAKE2b-256 checksum
How to use checksums
0da01356f7e523ec7edc7de4a2212a46f1bca46b110fd2bbfb85b981fd6627d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.6

Release files / cs_djutils-20241119-py3-none-any.whl

Download URL cs_djutils-20241119-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d20f93a62030b0658f26a7527a095512035a9add473d12f302b62dbc9a9d3e8e
BLAKE2b-256 checksum
How to use checksums
8bdf165262b7b5d0cdcd1aa721a831c895f02bc380ddc91cfc523e6d8cf1712c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page