Django: query time from datetime fields

In o postgresql db based on Django, how can I filter by time for the datetime field as shown below?

class Foo(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()

IE: I want to filter Foo objects using start_date "16:30" and "19:00" end_date.

Thank.

+5
source share
5 answers

How about adding to TimeField? http://docs.djangoproject.com/en/dev/ref/models/fields/#timefield

Otherwise, you will need to write your own query to take advantage of database time capabilities, as DateTimeFields do not have this feature natively in Django.

start_date start_time, start_time.

+2

q:

   def query_by_times(start_hour, start_min, end_hour, end_min):
        query = 'EXTRACT(hour from start_date) = %i and EXTRACT(minute from start_date) = %i and EXTRACT(hour from end_date) = %i and EXTRACT(minute from end_date) = %i' % (start_hour, start_min, end_hour, end_min)
        return Foo.objects.extra(where=[query])
+1

, , ...

:

from datetime import time
records = Foo.objects.all()
filtered = filter(lambda x: (time(16,30)==x.start_date.time() and time(19,0)==x.end_date.time()), records)

.

+1

Try

Foo.objects.filter(start_date = MyDate1, end_date = MyDate2)

MyDate1 MyDate2 - datetime. , ?

0

, [1].

[1] http://docs.djangoproject.com/en/1.2/ref/models/querysets/#range

-1
source

All Articles