python-3.xdjangocommanddjango-appsdjango-management-command

What's the real use of the management/commands directory in the Django Application?


In the documentation, it is written that it can be used for writing custom Django-admin commands. But my question is why do we need to write custom Django admin commands? The given example in the official documentation is a bit dry to me. I would be really grateful if someone give real-world examples from which I can connect its real-life use.

Django doc on management/commands:https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/


Solution

  • I mainly use it from Cron / Scheduled Tasks..

    Some potential examples would be:

    1. Sending out Reports/Emails
    2. Running Scripts to Update+Sync some Values
    3. Updating the Cache
    4. Any large update to values- save it to a command to run on the Prod Env
      • I make it + test it locally, but then I don't want to Copy+Paste it in a SSH terminal cause it sometimes gets all sorts of messed up in the paste.

    I also have a management command dothing that sets up the entire project.. runs migrations, collects static, imports db, creates test users, creates required folder structures, etc.

    I also have a couple of commands that I use, that I haven't made into Views.. Little tools to help me validate and clean data, spits out a representation of it