Birthday calculation failing silently
|Reported by:||nil_von_9wo||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I have a model:
class Fighter (models.Model): name = models.CharField(max_length=100) birth_date = models.DateField(blank=True, null=True) def age(self): return calculate_age(self.birth_date)
calculate_age is as follow:
def calculate_age(born): today = datetime.datetime.today() try: # raised when birth day is February 29 and the current year is not a leap year birthday = born.replace(year=today.year) except ValueError: birthday = born.replace(year=today.year, day=born.day-1) import pdb; pdb.set_trace(); if birthday > today: return today.year - born.year -1 else: return today.year - born.year
This function works perfectly from the command line.
However, as it is, from within a running django server, Fighter.age() returns no results.
By experimenting, I learned Fighter.age() will return a result if I return something (anything) in calculate_age() before it hits
if birthday > today:
By returning both values before that point, I verified that neither was "None", however they may not be the same data type.
I'm going to try casting them to match the type, but I was advised I should report this incident.