Implemented QuerySet.datetimes on PostgreSQL.

This commit is contained in:
Aymeric Augustin 2013-02-10 21:43:03 +01:00
parent 0c829c23f4
commit f6800fd04c
1 changed files with 17 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.conf import settings
from django.db.backends import BaseDatabaseOperations from django.db.backends import BaseDatabaseOperations
@ -36,6 +37,22 @@ class DatabaseOperations(BaseDatabaseOperations):
# http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
def datetime_extract_sql(self, lookup_type, field_name):
if settings.USE_TZ:
field_name = "%s AT TIME ZONE %%s" % field_name
# http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
if lookup_type == 'week_day':
# For consistency across backends, we return Sunday=1, Saturday=7.
return "EXTRACT('dow' FROM %s) + 1" % field_name
else:
return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
def datetime_trunc_sql(self, lookup_type, field_name):
if settings.USE_TZ:
field_name = "%s AT TIME ZONE %%s" % field_name
# http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
def deferrable_sql(self): def deferrable_sql(self):
return " DEFERRABLE INITIALLY DEFERRED" return " DEFERRABLE INITIALLY DEFERRED"