Skip to content

Instantly share code, notes, and snippets.

@arsatiki
Created August 13, 2009 08:21
Show Gist options
  • Save arsatiki/167051 to your computer and use it in GitHub Desktop.
Save arsatiki/167051 to your computer and use it in GitHub Desktop.
DAY, WEEK, MONTH, YEAR = map(timedelta, (1, 7, 30, 365))
def newer_than(fieldname, delta)
# positive values go to past, negative to future. counterintuitive?
time = datetime.now() - delta
fieldname = fieldname + "__gte"
return Q(**fieldname: past)
# For more granularity. Probably YAGNI
def newer_than2(fieldname, **delta_args)
return newer_than(fieldname, timedelta(**delta_args))
# example
Blog.objects.filter(newer_than('created', 2*WEEKS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment