Created
June 18, 2016 23:47
-
-
Save marcusshepp/1e7e990dc0753719e7c2f01819852b2e to your computer and use it in GitHub Desktop.
time since object creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DateCreatedAndUpdate(models.Model): | |
""" | |
Abstract class used to populate childern with a `date_created` field | |
""" | |
class Meta: | |
abstract, ordering = True, ("-id",) | |
date_created = models.DateTimeField(auto_now_add=True) | |
date_updated = models.DateTimeField(auto_now=True) | |
def time_past_since_creation(self): | |
time = (timezone.now()-self.date_created) | |
hours = time.seconds / 60 / 60 | |
weeks = time.days / 7 | |
return "Weeks: {weeks}, Days: {days}, Hours: {hours}, Seconds: {seconds}".format( | |
days=time.days, seconds=time.seconds, weeks=weeks, hours=hours | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment