I have
class Person(TimeStampedModel, Address):
name = models.CharField(_('nome'), max_length=100)
birthday = models.DateField(_('data de nascimento'))
and this function
import datetime
from datetime import date
'''
http://keyes.ie/calculate-age-with-python/
'''
def age(when, on=None):
if on is None:
on = datetime.date.today()
was_earlier = (on.month, on.day) < (when.month, when.day)
return on.year - when.year - (was_earlier)
age(date(2000, 1, 1))
How to count person per age
age quant
20-25 89
25-30 100
30-35 90
35-40 102
I know I have to use annottate and count , but do not know where to start.
In your case I would use 2 functions, one to obtain the age of the person, and another to prepare a dict that return persons grouped by age range.
An useful tip would be to add the
calculate_age
function to yourPerson
model as a property like:So you when you have a person object, you could do:
and the return would be the age in years