Select between two dates with Django


Question

I am looking to make a query that selects between dates with Django.

I know how to do this with raw SQL pretty easily, but how could this be achieved using the Django ORM?

This is where I want to add the between dates of 30 days in my query:

start_date = datetime.datetime.now() + datetime.timedelta(-30)
context[self.varname] = self.model._default_manager.filter(
    current_issue__isnull=True
    ).live().order_by('-created_at')
1
107
12/19/2015 7:49:18 AM

Accepted Answer

Use the __range operator:

...filter(current_issue__isnull=True, created_at__range=(start_date, end_date))
210
3/31/2014 12:58:52 PM


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon