tags:

views:

43

answers:

1

Let's say I have the following table:

Employee

name      start_date       end_date
John      2009-10-10       2009-12-31
Joe       2009-12-01       2010-05-10

I also have a curr_date = '2009-11-01'. If I want to get all employees where curr_date should be between an employee's start_date and end_date inclusive, the corresponding SQL is:

SELECT * 
FROM employee
WHERE curr_date between start_date and end_date

The corresponding Django QuerySet I came up with is:

Employee.objects.filter(start_date__lte=curr_date, end_date__gte=curr_date)

Is there an alternate way to do the above with Django's QuerySet, the __range method doesn't quite work for the above case.

A: 

You're right, the __range method won't work, so no, there is no way to use the Django ORM to create the SQL you are after.

Is there a problem with using the .filter method which you came up with?

SmileyChris
There's nothing wrong with the filter, I was just wondering of maybe a DRYer way of performing this query, I guess you can't really DRY everything.
Thierry Lam