In my django(3.0.5) application I am trying to get average age from date of birth in a model. I am using MySql database. Here is how I tried:
Model:
class ShippingStaff(models.Model):
full_name = models.CharField('Full Name', max_length=200)
birth_date = models.DateField('Date of Birth', null=True, blank=True)
Customer Filter:
@register.filter(name='age')
def age(bday, d=None):
if d is None:
d = datetime.date.today()
return (d.year - bday.year) - int((d.month, d.day) < (bday.month, bday.day))
Views:
def home(request):
shipping_staff = ShippingStaff.objects.aggregate(staff_count=Count('full_name'),
avg_age=Avg(custom_time.age('birth_date'))
I get error like:
Exception Value:
'str' object has no attribute 'year'
Exception Location: /home/smrashel/jahaji/crewdatabd/templatetags/custom_time.py in age, line 31
which is
return (d.year - bday.year) - int((d.month, d.day) < (bday.month, bday.day))
How can I solve this? Any help will be much appreciated.