Django: Filter results within date range

I needed to add some sort of date ranging filter into my query. To do that, we first calculate the date range.

import datetime

six_months_ago = (datetime.date.today() - datetime.timedelta(6 * 365 / 12))

This simply figures out the date which was 6 months from today.

To use that to filter a date field (without time information):

SomeModel.objects.filter(create_date__gte = six_months_ago.isoformat())

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog