views:

44

answers:

1

I saw the next two method on old question here but it is not clear for me what is the difference between:

{'date_time_field__range': (datetime.datetime.combine(date, datetime.time.min),
                        datetime.datetime.combine(date, datetime.time.max))}

and

YourModel.objects.filter(datetime_published__year='2008', 
                     datetime_published__month='03', 
                     datetime_published__day='27')
+1  A: 

Was confused about this myself, but I think I've worked it out :-D I found the documentation about the range lookup option very helpful.

When you do:

YourModel.objects.filter(datetime_published__year='2008', 
                     datetime_published__month='03', 
                     datetime_published__day='27')

The SQL will look something like:

SELECT ... WHERE EXTRACT('year' FROM pub_date) = '2008'
             AND EXTRACT('month' FROM pub_date) = '03'
             AND EXTRACT('day' FROM pub_date) = '27';

Whereas this part of django's generic date based views:

{'date_time_field__range': (datetime.datetime.combine(date, datetime.time.min),
                        datetime.datetime.combine(date, datetime.time.max))}

becomes something like:

YourModel.objects.filter(datetime_published__range=(
                datetime.datetime.combine('2008-03-27',datetime.time.min),
                datetime.datetime.combine('2008-03-27',datetime.time.max)
                                                                  )

which produces SQL along the lines of:

SELECT ... WHERE datetime_published BETWEEN '2008-03-27 00:00:00'
                                        AND '2008-03-27 23:59:59';

(the format of the timestamp in the last SQL example is wrong obviously, but you get the idea)

Hope that answers your question :)

Monika Sulik