From 1c360dbbf56b76e1df7c945458ae2987306fcfcd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= <akaariai@gmail.com>
Date: Mon, 20 Jan 2014 05:02:29 +0200
Subject: [PATCH] Use date_extract_sql() for DateFields

Refs #21821
---
 django/db/models/lookups.py | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index d73418b5af..ce6d157f82 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -281,12 +281,15 @@ default_lookups['range'] = Range
 
 
 class DateLookup(BuiltinLookup):
-
     def process_lhs(self, qn, connection, lhs=None):
+        from django.db.models import DateTimeField
         lhs, params = super(DateLookup, self).process_lhs(qn, connection, lhs)
-        tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
-        sql, tz_params = connection.ops.datetime_extract_sql(self.extract_type, lhs, tzname)
-        return connection.ops.lookup_cast(self.lookup_name) % sql, tz_params
+        if isinstance(self.lhs.output_type, DateTimeField):
+            tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
+            sql, tz_params = connection.ops.datetime_extract_sql(self.extract_type, lhs, tzname)
+            return connection.ops.lookup_cast(self.lookup_name) % sql, tz_params
+        else:
+            return connection.ops.date_extract_sql(self.lookup_name, lhs), []
 
     def get_rhs_op(self, connection, rhs):
         return '= %s' % rhs