0

Here is my Django Model:

from django.db import models
import datetime

class MyModel(models.Model):
    my_timestamp = models.DateTimeField(
        default=datetime.datetime(
            year=2016, 
            month=10, 
            day=3, 
            hour=9, 
            minute=3, 
            second=1,
        ), 
        null=False, 
        blank=False, 
        editable=False,
    )

I would like to trigger a certain action when the system's current date/time equals the value of any instance's my_timestamp field. What is the best way to do it? I could run Chron job that continuously ran and polled the all the instances. But that seems messy. Is there a way I can trigger this event without constant wasteful polling?

Can I register some kind of callback that would accomplish this for me?

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • What is the resolution at which you want to trigger the check? Every second? Every minute? Every 5 minutes? Any callback you could register would have to be executed at that resolution - callbacks are anyways event based not time based. – Burhan Khalid Jan 17 '16 at 08:01
  • I'm not sure. Suppose it was every minute. Can it be done? – Saqib Ali Jan 17 '16 at 08:19
  • Yes it can be done; and to answer your next question on optimization - this will depend on the volume of data and what kind of update you want to do. – Burhan Khalid Jan 17 '16 at 08:21

2 Answers2

2

Django signals provide this functionality. They allow you to hook into multiple parts of Django, model CRUD being one of them.

Take a look at https://docs.djangoproject.com/en/1.9/topics/signals/

For a list of signals: https://docs.djangoproject.com/en/1.9/ref/signals/

bryanph
  • 993
  • 7
  • 18
1

You can use Celery Periodic Tasks or create custom manage.py command and use raw cron to start it.

inlanger
  • 2,904
  • 4
  • 18
  • 30