manch manch - 5 months ago 16
Python Question

Best way to trigger a function when DateField in model expires

I have a "Contest" model that a user creates and one of the fields is

(endTime = models.DateField(default=datetime.now()+timedelta(days=7))
.

I need a method to run a function when their contest has expired. The function would be to notify users, update objects, etc.

What would be the best method to achieve this?

Answer

To schedule a task you can look here

as for a this question,

procedure to identify that the datetime field has passed

This would work:

Contest.objects.filter(endTime__lte=timezone.now())

So you would have a task running continuously that would call a django command every X hours, that would use the above search to find any expired contests.