Hello,
for a project I am using Django management commands to create a CLI. Until now the project is really basic, nothing fancy.
My starting point is python manage.py start
:
# start.py
from django.core.management.base import BaseCommand
from django.core.management import call_command
from cli.menu import get_start_menu
class Command(BaseCommand):
help = "Start of the program."
def handle(self, *args, **options):
choice = get_start_menu("Welcome to Epic Events")
if choice == 1:
call_command("account_manager")
if choice == 2:
call_command("contract")
if choice == 3:
call_command("event")
get_start_menu is just creating a nice menu for the terminal.
This menu is working fine, the call_command() methods are working as expected.
My choice will be 1, so I call account_manager.py
:
# account_manager.py
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.core.management.base import BaseCommand
from cli.menu import get_app_menu
UserModel = get_user_model()
class Command(BaseCommand):
help = "Menu for all operations around the account managers."
def handle(self, *args, **options):
choice = get_app_menu("account manager")
all_users = UserModel.objects.all()
if choice == 4:
call_command("start")
if choice == 1:
call_command(
"account_manager_create",
user=all_users[1],
first_name="John",
last_name="Doe",
role="Management",
)
When my second choice will be 1 again, I want to call the command 'account_manager_create", I get this output:
Traceback (most recent call last):
File "/home/Desktop/P12/manage.py", line 22, in <module>
main()
File "/home/Desktop/P12/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/Desktop/P12/cli/management/commands/start.py", line 14, in handle
call_command("account_manager")
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 194, in call_command
return command.execute(*args, **defaults)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/Desktop/P12/accounts/management/commands/account_manager.py", line 21, in handle
call_command(
File "/home/Desktop/P12/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 164, in call_command
parse_args.append(min(opt.option_strings))
^^^^^^^^^^^^^^^^^^^^^^^
ValueError: min() arg is an empty sequence
What have I tried:
-
I have deleted one argument, two arguments three arguments inside the
call_command( "account_manager_create", user=all_users[1], first_name="John", last_name="Doe", role="Management", )
the result is the same output of the traceback as above. -
If I delete all the arguments inside the
call_command( "account_manager_create", user=all_users[1], first_name="John", last_name="Doe", role="Management", )
the result is this traceback (what makes totally sense):
CommandError: Error: the following arguments are required: user, first_name, last_name, role
I do not understand and can not find out why I get the output: ValueError: min() arg is an empty sequence
when calling the command ‘account_manager_create’.
I am using: Django==5.0
This is the ‘account_manager_create.py’ file:
# account_manager_create.py
from django.core.management.base import BaseCommand
from accounts.models import AccountManager
class Command(BaseCommand):
help = "Creates a new account manager."
def add_arguments(self, parser):
parser.add_argument("user", type=int, help="Enter the user id.")
parser.add_argument("first_name", type=str, help="Enter the first name.")
parser.add_argument("last_name", type=str, help="Enter the last name.")
parser.add_argument("role", type=str, help="Enter the role.")
def handle(self, *args, **options):
account_manager = AccountManager(
user=options["user"],
first_name=options["first_name"],
last_name=options["last_name"],
role=options["role"],
)
account_manager.save()
self.stdout.write("New account manager was created.")
I have gotten the hint: “it seems like the issue might be related to how Django’s call_command()
function handles arguments”
Is that possible or can you point to my mistake in my code? I would like to know what the problem is and where I can check to resolve this issue.
Cheers,
Doro