In my django(3.0.5) application I am trying to get average age from date of birth in a model. In MySql database table birth_date stores data in ‘1980-01-15’ format. Here is how I tried:
class ShippingStaff(models.Model): full_name = models.CharField('Full Name', max_length=200) birth_date = models.DateField('Date of Birth', null=True, blank=True) @register.filter(name='age') def age(bday, d=None): bday = datetime.strptime(bday, '%Y-%m-%d') if d is None: d = datetime.date.today() return (d.year - bday.year) - int((d.month, d.day) < (bday.month, bday.day)) def home(request): shipping_staff = ShippingStaff.objects.aggregate(staff_count=Count('full_name'), avg_age=Avg(custom_time.age('birth_date'))
|Exception Value:||time data ‘birth_date’ does not match format ‘%Y-%m-%d’|
How can I solve this? Any help will be much appreciated.