1

I heard of about Django Custom Management script in lots of community and i am not getting why should i write custom management script as django manage.py a lot of useful command? I am very new in django...

Can anyone tell me some usecase of custom management script?

Thanks

  • 1
    You might want to do certain tasks periodically, like cleaning up records. You can put that logic in a command, and then let something run that command on a daily basis, for example. – Willem Van Onsem Jul 04 '19 at 17:08

1 Answers1

1

The documentation on Writing custom django-admin commands mentions:

Applications can register their own actions with manage.py. For example, you might want to add a manage.py action for a Django app that you’re distributing

Usually those commands are used for "maintenance" activities. Or to give administrators more convenient tooling.

For example, you can use django-unused-media [GitHub]. This will add an extra admin command, that you can use like:

./manage.py cleanup_unused_media --noinput

This will then remove media files that are no longer referenced. You can then make a cronjob that for example each week calls that command to clean up media files. It thus here can help you to reduce disk space usage, without having to interfere yourself.

Another use case is to make management of your server more convenient. In fact makemigrations [GitHub], migrate [GitHub], makemessages [GitHub] and runserver [GitHub] are managment commands as well. So you probably use these commands all the time.

You put a certain amount of logic into these that is useful, and that you want to repeat several times. So instead of defining migration files yourself each time you change your model, you can implement the logic once to write such commands, and then the developer/administrator, can run a convenient script to do some work.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555