0

I have a model that takes an expires_at time field. In the sense, the user can set a time beyond which the entry is invalid. At that time, I need to do specific actions such as send email and/or change a parameter in the model.

Specifically, say I have a model thus:

class SomeModel(models.Model):
    ... #Code here ...
    expires_at = models.DateTimeField()

Now, when the model is saved, there will be a time set in the expires_at field that is some time in the future. When this time occurs, I need to send an email.

I have looked at the signals feature of django, but haven't been very successful. I don't want to resort to running cronjobs that keep polling the database or something. Can someone point me in the right direction?

Sidd
  • 1,168
  • 2
  • 10
  • 27
  • 1
    cronjobs do poll the database repeatedly, but it would not be a huge performance hit, since you would be querying for what has changed since the last execution of the cron job. – karthikr Apr 04 '13 at 20:52

2 Answers2

0

APScheduler would do the trick - see here: http://pythonhosted.org/APScheduler/dateschedule.html

Greg
  • 9,963
  • 5
  • 43
  • 46
0
class SomeModel(models.Model):
    ... #Code here ...
    expires_at = models.DateTimeField()


def your_signal(sender, instance, created, **kwargs):
    if created:
        instance.expires_at =       <---set time here
        instance.save()

        #do other stuff

post_save.connect(your_signal, sender=SomeModel)
catherine
  • 22,492
  • 12
  • 61
  • 85
  • Hey. This is fine, if I just wanted to set the expiration date when the instance is created. My question was to _send an email_ at the expiration time. – Sidd Apr 08 '13 at 01:20
  • That's simple just add send email function on the signal, can't you get what I'm trying to portrait in my answer? – catherine Apr 08 '13 at 06:04