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

Time:09-15

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/

CodePudding user response:

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

CodePudding user response:

Django scheduled operations and report generation from cron is the obvious one.

Another I use is for loading data into the DB from csv files. It's easy in the management command environment to handle bad rows. I write the original csv row into an exceptions file (with a error-description column appended) and can then look at it and decide what to do about these rows. Sometimes, just a trivial edit and feed it through the management command again. It's possible to do the same via a view, but extra work for IMO no gain.

  • Related