Monday, February 8, 2016

Running automated expiration date events

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?

Here is an example:

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.

Is this possible?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87c2360f-b02c-42d4-ac33-d1dcdf4b852a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment