[Answer]-Python/Django Calculate Expiration for Model

1👍

You can use the timeuntil() function which is used for the timeuntil template filter:

from django.utils.timesince import timeuntil

def expiration(self):
    return timeuntil(self.date_offered)

0👍

I used this method to determine if my user was an adult (i.e., at least 18 years old). You should be able to manipulate it for your needs.

from datetime import date


def set_adult(self):
    today = date.today()
    age = today.year - self.date_of_birth.year - ((today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day))
    if age >= 18:
      self.adult = True
    else:
      self.adult = False
👤erip

Leave a comment