On Wed, May 9, 2012 at 12:12 AM, Lachlan Musicman <data...@gmail.com> wrote: > Hola, > > I have a model Person with a dob = models.DateField() > > I would like to filter by age - in particular only include people less than > 60 years old. > > I am having no luck getting the syntax right and was looking for pointers? > > queryset=Person.objects.filter(dob__year__gte > = datetime.datetime.today().year-61) > > gets me an error like: Join on field 'dob' not permitted. Did you misspell > 'year' for the lookup type? > > RETIRE_Y = datetime.datetime.today().year-61 > ... > queryset=Person.objects.filter(dob__gte = > datetime(RETIRE_Y,1,1)))), > > gives me errors like: 'module' object is not callable > > I don't want to create a new field or function on the Person object that > would calculate "age" everyday... > > What am I doing wrong/how can I make it right? >
You're looking at the data the wrong way. Instead of trying to filter out people whose age is over 60, filter out people who were born over 60 years ago: latest_retirees_dob = datetime.datetime.now() - datetime.timedelta(years=60) still_working = Person.objects.filter(dob__gt=latest_retirees_dob) retired = Person.objects.filter(dob__lte=latest_retirees_dob) Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.