Re: Filter by retirement age?
Thanks Tom, but I get an error because timedelta doesn't take years - I've changed it to weeks=3120 and it works.
Because of that error I've also found another thread discussing the issue:
I liked the most popular answer's standard lib use of from_date.replace - is there a norm regards where a small utility like that would be put? It feels wrong putting it in [views|admin|models|url].py ..?
cheers
L.
On Wednesday, May 9, 2012, Tom Evans wrote:
On Wed, May 9, 2012 at 12:12 AM, Lachlan Musicman <datakid@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home