7

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 =)

3 Answers 3

4
  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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

-2

If you're on a UNIX-like machine, it's possible that you have access to cronjobs. If you're on Windows, I hear there's a program called at that can do similar things. If this doesn't suit your needs, there are a number of ways to do things every X hours using the time library (time.sleep(SOME_NUMBER_OF_SECONDS) in a loop with whatever else you want to do will do it if you want something done regularly, otherwise you'll need to look at time.localtime() and check for conditions).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.