I have a simple article model with pub_date field and a unpublish_date field.
Is there any way to automagically set my is_published field to False when the unpublish_date is in the past?
class Article(models.Model):
title = models.CharField(max_length=500)
post = models.TextField(blank=True, null=True,)
pub_date = models.DateTimeField(default=datetime.datetime.now)
unpublish_date = models.DateTimeField(blank=True, null=True)
is_published = models.BooleanField(default=True,)
You can simply factor this
unpublish_dateinto your queries, for example:This is likely to make your code more complicated, though. You’d have to rewrite all your queries to be aware of the
unpublish_date.Cron works for this sort of thing but is only good for changing things in large batches once or a few times a day. It doesn’t scale well when you have a large batch of tasks to do or want more fine-grained scheduling.
If running a task via cron once per day is sufficient, do that. It might be worth investigating more advanced options such as message queues, though.
To make a scheduled task which will run at a precise time (provided a worker is available), I would use Celery.