On Apr 30, 11:26 am, Derek <gamesb...@gmail.com> wrote: > I would like to be able to filter a query set based on a calculated date > value. Googling did not bring up anything obvious. > > As close as I can express it in "pseudo syntax", I'd like to do something > along these lines: > > import datetime > from dateutil.relativedelta import * > today = datetime.now() > MyModel.objects.filter( (my_date_field + relativedelta(months = > +my_month_field))__lt = today ) > > i.e. compare one date with another - the second is calculated using a date > field and a second numeric field (value representing months). > > The above syntax does not work - I am not sure how to code the calculation > part... > > Thanks > Derek >
The calculation needs to go on the other side of the expression. The left-hand side is a keyword, so can't be a calculation. And you can use the F() function to get the value of your months field. I think this should work: from django.db.models import F MyModel.objects.filter(my_date_field__lt=today - relativedelta(months=F('my_month_field'))) -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.