I have a field of a model that changes states but I need to automatically modify that value after an expiration datetime has come. Is there a way to do this?
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
languages = (
('1', 'active'),
('2', 'inactive'),
)
language = models.CharField(max_length=20, choices=languages, default='english')
duration = models.DurationField(blank=True, null=True)
expires = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
@property
def active(self):
return self.expires > localtime(now())
def save(self, *args, **kwargs):
self.created_at = localtime(now())
self.expires = self.created_at + self.duration
return super(Post, self).save(*args, **kwargs)
The problem I have is that I have to call the active function property to check if it's True or False and I'm looking for a way that this can be done automatically so the database updates itself.
I've been thinking about Django-cron but I can't chose when to call the active function, this should be done for itself.