Hi,
I have a question about creating a new custom Django command. I don’t want to create it as described in the link below. I want to implement it within the Django core. Is this possible, and if so, how?
https://docs.djangoproject.com/en/5.1/howto/custom-management-commands/
Here is one example:
> python manage.py
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
optimizemigration
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
MyCustomCommand <----- new here
It’s possible, but would create bigger headaches for you in future.
Why don’t you want to create a management command within an app? In understanding this, I may be able to give a better answer.
Hi @nanorepublica,
To better understand my point of view, please take a look at my GitHub project. It’s designed to handle switching between environments. I want to convert this project into a command-line tool instead of relying on the environment.py
file. Additionally, I plan to turn it into a pip package so it can work across all projects in the future. However, I’m unsure how to approach this in a smart and efficient way.
https://github.com/zlaja-billund/django-easy-local-prod-environment-switch
Ok, but that still doesn’t answer the ‘why’ the management command cannot be in an app of itself?
I would expect this especially if you want to release this as a package.
Ok, I understand your point of view. My new question is: how can I create a new app command when I install a pip package? For example, if I run pip install custom-made-package
, should it automatically create a new folder inside the app folder? Or can I keep the management command inside the package folder while still making it accessible through manage.py
?
manage.py will list any management commands for apps that are listed in INSTALLED_APPS
The easiest way to see this in action would be to install the package django-extensions
which provides a number of extra helpful commands, but will also allow you to see what happens when a package is installed with management commands.
2 Likes
Okay, now I understand what you think, and I found the documentation with the right answer I was searching for. I’ll try this method, and I think it will work. Thanks for helping me rethink the way to do it.
https://docs.djangoproject.com/en/5.1/intro/reusable-apps/
1 Like