0

So this question is focused more around best practice and advice for Django.

Essentially, I want to schedule email reports on Django to be triggered on two events:

  • Weekly report email with some stats, news etc
  • Report triggered on event in a system (i.e. a save on a model)

Should this be done directly in Django through scheduled tasks? Or are there any other tools one could use?

NickP
  • 1,354
  • 1
  • 21
  • 51

1 Answers1

1

Regarding a weekly scheduled task, the most straightforward approach might be to create a new custom management command, and have a cron or Windows Task Scheduler call that command. This actually already has an answer here, along with other possible options for you to consider:

Django - Set Up A Scheduled Job?

Note: If you're using a virtualenv, make sure to have the cron call the management command via the python binary in the virtualenv, not the one in the system path.

As for a triggered action based on an application event or condition, two thoughts:

  • You could set up a listener for a post-save signal on your model. When the signal is received, the email could be sent via the receiver. You can read up on signals here.
  • Django's send_mail email wrapper is straightforward enough that you could also use that directly in your view.
trpt4him
  • 1,646
  • 21
  • 34
  • Thanks for the answer. I already have a number of scheduled tasks on different cadence's so aware this approach exists. However, after more specific question if it's best to do this sort of reporting in Django and/or if not, how does one hook a reporting tool to trigger and interface with Django. – NickP Aug 01 '17 at 17:58
  • That may depend widely on what exactly you want to report on. Again I think the most basic and easiest "way in" to a Django app from the OS or 3rd party tool is via the management commands. You could write what you want to report to the database via the external tool, then use Django to format and deliver the report. – trpt4him Aug 02 '17 at 16:54