How to correctly add a custom '--dry-run' argument to a Django Extensions `runjob` command?

I have a custom Django management job created using django_extensions that deletes old database records. To avoid unintended deletions, I want to add a --dry-run argument to simulate deletions without actually removing data. However, when I execute the command with:

uv run python manage.py runjob delete_recordings --dry-run

I receive this error:

manage.py runjob: error: unrecognized arguments: --dry-run

Here's how my simplified job class currently looks:

from django_extensions.management.jobs import HourlyJob
import logging

logger = logging.getLogger(__name__)

class Job(HourlyJob):

    @classmethod
    def add_arguments(cls, parser):
        parser.add_argument(
            '--dry-run',
            action='store_true',
            help='Execute the job in simulation mode without deleting any data.',
            default=False,
        )

    def execute(self, *args, **options):
        dry_run = options.get('dry-run', False)
        if dry_run:
            logger.info("Executing in DRY-RUN mode.")
        # Logic here to delete records or simulate deletion based on dry_run

I followed the Django Extensions documentation to add a custom argument (--dry-run). I expected that when running the command with --dry-run, it would recognize the argument and simulate the operation, logging the intended deletions without performing them.

However, the command line returns an error indicating that the argument is not recognized. It seems that Django Extensions isn't picking up the custom argument defined in my job class.

What is the correct way to add a custom argument (--dry-run) to a Django Extensions runjob command?

Вернуться на верх