6

I want to find a custom command in a project with many apps, how to get a list of all commands from all apps?

3
  • 3
    Just use python3 manage.py. This will list all possible commands. Commented Mar 28, 2022 at 14:02
  • Indeed! I was so sure, that only built-in commands would be listed Commented Mar 28, 2022 at 14:08
  • 1
    well to some extent, there are no "builtin" commands: Django uses apps as well, so the auth app for example exports commands to create users. These are also defines, just like the ones you create yourself. Commented Mar 28, 2022 at 14:34

2 Answers 2

14

This command will list all the custom or existing command of all installed apps:

python manage.py help
Sign up to request clarification or add additional context in comments.

Comments

7

You can also load django commands using a django module.

To get a list of all custom Django commands in a project, you can use the from django.core.management import get_commands. This get_commands function returns a dictionary of all available commands and their associated applications from the running application.

Here is an example of how you can use this function to display all commands and their associated applications:

from django.core.management import get_commands

commands = get_commands()
print([command for command in commands.items()])
#sample output 

$ [('check', 'django.core'),
 ('compilemessages', 'django.core'),
 ('createcachetable', 'django.core'),
 ('dbshell', 'django.core'),
 ('diffsettings', 'django.core'),
 ('dumpdata', 'django.core'),
 ('flush', 'django.core'),
 ('inspectdb', 'django.core'),
 ('loaddata', 'django.core'),
 ('makemessages', 'commands'),
 ('makemigrations', 'django_migration_linter'),
 ('migrate', 'django.core'),
 ('runserver', 'django.contrib.staticfiles'),
 ('sendtestemail', 'django.core'),
 ('shell', 'django.core'),
]

If you want to display only the commands for a specific application, you can filter the results of get_commands() using the following code:

[command for command in commands.items() if command[1] == 'app_name']

Replace 'app_name' with the name of the application you want to display the commands for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.