Ticket #7672: 7672.diff
File 7672.diff, 8.9 KB (added by , 16 years ago) |
---|
-
django/db/models/sql/where.py
174 174 params) 175 175 elif lookup_type in ('range', 'year'): 176 176 return ('%s BETWEEN %%s and %%s' % field_sql, params) 177 elif lookup_type in ('month', 'day' ):178 return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, 179 field_sql),params)177 elif lookup_type in ('month', 'day', 'week_day'): 178 return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql), 179 params) 180 180 elif lookup_type == 'isnull': 181 181 return ('%s IS %sNULL' % (field_sql, 182 182 (not value_annot and 'NOT ' or '')), ()) -
django/db/models/sql/constants.py
4 4 QUERY_TERMS = dict([(x, None) for x in ( 5 5 'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in', 6 6 'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year', 7 'month', 'day', ' isnull', 'search', 'regex', 'iregex',7 'month', 'day', 'week_day', 'isnull', 'search', 'regex', 'iregex', 8 8 )]) 9 9 10 10 # Size of each "chunk" for get_iterator calls. -
django/db/models/fields/__init__.py
201 201 sql, params = value.as_sql() 202 202 return QueryWrapper(('(%s)' % sql), params) 203 203 204 if lookup_type in ('regex', 'iregex', 'month', 'day', ' search'):204 if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): 205 205 return [value] 206 206 elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'): 207 207 return [self.get_db_prep_value(value)] … … 492 492 def get_db_prep_lookup(self, lookup_type, value): 493 493 # For "__month" and "__day" lookups, convert the value to a string so 494 494 # the database backend always sees a consistent type. 495 if lookup_type in ('month', 'day' ):495 if lookup_type in ('month', 'day', 'week_day'): 496 496 return [force_unicode(value)] 497 497 return super(DateField, self).get_db_prep_lookup(lookup_type, value) 498 498 -
django/db/backends/postgresql/operations.py
26 26 27 27 def date_extract_sql(self, lookup_type, field_name): 28 28 # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT 29 return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) 29 if lookup_type == 'week_day': 30 # Using EXTRACT(), PostgreSQL days are indexed as Sunday=0, Saturday=6. 31 # If we instead us TO_CHAR, they're indexed with Sunday=1, Saturday=7 32 return "TO_CHAR(%s, 'D')" % field_name 33 else: 34 return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name) 30 35 31 36 def date_trunc_sql(self, lookup_type, field_name): 32 37 # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC -
django/db/backends/sqlite3/base.py
207 207 dt = util.typecast_timestamp(dt) 208 208 except (ValueError, TypeError): 209 209 return None 210 return unicode(getattr(dt, lookup_type)) 210 if lookup_type == 'week_day': 211 return unicode((dt.isoweekday() % 7) + 1) 212 else: 213 return unicode(getattr(dt, lookup_type)) 211 214 212 215 def _sqlite_date_trunc(lookup_type, dt): 213 216 try: -
django/db/backends/mysql/base.py
116 116 class DatabaseOperations(BaseDatabaseOperations): 117 117 def date_extract_sql(self, lookup_type, field_name): 118 118 # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html 119 return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 119 if lookup_type == 'week_day': 120 # DAYOFWEEK() returns an integer, 1-7, Sunday=1. 121 # Note: WEEKDAY() returns 0-6, Monday=0. 122 return "DAYOFWEEK(%s)" % field_name 123 else: 124 return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 120 125 121 126 def date_trunc_sql(self, lookup_type, field_name): 122 127 fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] -
django/db/backends/oracle/base.py
72 72 73 73 def date_extract_sql(self, lookup_type, field_name): 74 74 # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163 75 return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 75 if lookup_type == 'week_day': 76 # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday. 77 return "TO_CHAR(%s, 'D')" % field_name 78 else: 79 return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 76 80 77 81 def date_trunc_sql(self, lookup_type, field_name): 78 82 # Oracle uses TRUNC() for both dates and numbers. … … 268 272 self.connection = Database.connect(conn_string, **self.options) 269 273 cursor = FormatStylePlaceholderCursor(self.connection) 270 274 # Set oracle date to ansi date format. This only needs to execute 271 # once when we create a new connection. 275 # once when we create a new connection. We also set the Territory 276 # to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR(). 272 277 cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' " 273 "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'") 278 "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF' " 279 "NLS_TERRITORY = 'AMERICA'") 274 280 try: 275 281 self.oracle_version = int(self.connection.version.split('.')[0]) 276 282 # There's no way for the DatabaseOperations class to know the -
tests/modeltests/basic/models.py
74 74 <Article: Area woman programs in Python> 75 75 >>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28) 76 76 <Article: Area woman programs in Python> 77 >>> Article.objects.get(pub_date__week_day=5) 78 <Article: Area woman programs in Python> 77 79 78 80 # The "__exact" lookup type can be omitted, as a shortcut. 79 81 >>> Article.objects.get(id=1) … … 88 90 >>> Article.objects.filter(pub_date__year=2005, pub_date__month=7) 89 91 [<Article: Area woman programs in Python>] 90 92 93 >>> Article.objects.filter(pub_date__week_day=5) 94 [<Article: Area woman programs in Python>] 95 >>> Article.objects.filter(pub_date__week_day=6) 96 [] 97 91 98 # Django raises an Article.DoesNotExist exception for get() if the parameters 92 99 # don't match any object. 93 100 >>> Article.objects.get(id__exact=2) … … 100 107 ... 101 108 DoesNotExist: Article matching query does not exist. 102 109 110 >>> Article.objects.get(pub_date__week_day=6) 111 Traceback (most recent call last): 112 ... 113 DoesNotExist: Article matching query does not exist. 114 103 115 # Lookup by a primary key is the most common case, so Django provides a 104 116 # shortcut for primary-key exact lookups. 105 117 # The following is identical to articles.get(id=1). -
docs/ref/models/querysets.txt
1344 1344 Note this will match any record with a pub_date on the third day of the month, 1345 1345 such as January 3, July 3, etc. 1346 1346 1347 week_day 1348 ~~~~~~~~ 1349 1350 For date/datetime fields, a 'day of the week' match. 1351 1352 Example:: 1353 1354 Entry.objects.filter(pub_date__week_day=2) 1355 1356 SQL equivalent:: 1357 1358 SELECT ... WHERE EXTRACT('dow' FROM pub_date) = '2'; 1359 1360 (The exact SQL syntax varies for each database engine.) 1361 1362 Note this will match any record with a pub_date that falls on a Monday (day 2 1363 of the week), regardless of the month or year in which it occurs. Week days 1364 are indexed with day 1 being Sunday and day 7 being Saturday. 1365 1347 1366 isnull 1348 1367 ~~~~~~ 1349 1368