Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django execute task on time specified in model datetime field

got a simple question, I believe, but it got me stuck anyways.

Say I have a simple model:

class myModel(models.Model):
    expires = models.DateTimeField(...)

and I want, say on the specified time do something: send an email, delete model, change some of the models fields... Something. Is there a tool in django core, allowing me to do so?

Or, if not, I think some task queuing tool might be in order. I have djcelery working in my project, though I'm a completely newbie in it, and all I was able to perform so far, is to run django-celery-email package, in order to send my mail asynchronically. Though I can't say I'm fully capable of defining task and workers to work in background and be reliable.

If any ideas, on how to solve such problem, please, do not hesitate =)

like image 431
Sergey Aganezov jr Avatar asked Jul 10 '13 10:07

Sergey Aganezov jr


2 Answers

  1. Write a custom management command to do the task that you desire. When you are done, you should be able to run your task with python manage.py yourtaskname.

  2. Use cron, at, periodic tasks in celery, django-cron, djangotaskscheduler or django-future to schedule your tasks.

like image 137
Burhan Khalid Avatar answered Nov 04 '22 11:11

Burhan Khalid


I think the best is a background-task the reads the datime and executes a task if a datetime is or has been reached.

See the solution given here for a scheduled task

So the workflow would be:

  • Create the task you want to apply on objects whose date has been reached
  • Create a managment command that checks the datetimes in your DB, and execute the above task for every object the datetime has been reached
  • Use cron (Linux) or at(Windows) to schedule the command call
like image 40
Ricola3D Avatar answered Nov 04 '22 11:11

Ricola3D