Ticket #7672: dayofweek_filter.r7875.r4.diff
File dayofweek_filter.r7875.r4.diff, 8.5 KB (added by , 16 years ago) |
---|
-
django/db/models/sql/where.py
152 152 params) 153 153 elif lookup_type in ('range', 'year'): 154 154 return ('%s BETWEEN %%s and %%s' % field_sql, params) 155 elif lookup_type in ('month', 'day' ):155 elif lookup_type in ('month', 'day', 'week_day'): 156 156 return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, 157 157 field_sql), params) 158 158 elif lookup_type == 'isnull': -
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
233 233 if hasattr(value, 'as_sql'): 234 234 sql, params = value.as_sql() 235 235 return QueryWrapper(('(%s)' % sql), params) 236 if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', ' search'):236 if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'week_day', 'search'): 237 237 return [value] 238 238 elif lookup_type in ('range', 'in'): 239 239 return value -
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
151 151 dt = util.typecast_timestamp(dt) 152 152 except (ValueError, TypeError): 153 153 return None 154 return str(getattr(dt, lookup_type)) 154 if lookup_type == 'week_day': 155 return str((dt.isoweekday() % 7) + 1) 156 else: 157 return str(getattr(dt, lookup_type)) 155 158 156 159 def _sqlite_date_trunc(lookup_type, dt): 157 160 try: -
django/db/backends/mysql/base.py
68 68 class DatabaseOperations(BaseDatabaseOperations): 69 69 def date_extract_sql(self, lookup_type, field_name): 70 70 # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html 71 return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 71 if lookup_type == 'week_day': 72 # DAYOFWEEK() returns an integer, 1-7, Sunday=1. 73 # Note: WEEKDAY() returns 0-6, Monday=0. 74 return "DAYOFWEEK(%s)" % field_name 75 else: 76 return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 72 77 73 78 def date_trunc_sql(self, lookup_type, field_name): 74 79 fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] -
django/db/backends/oracle/base.py
56 56 57 57 def date_extract_sql(self, lookup_type, field_name): 58 58 # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163 59 return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 59 if lookup_type == 'week_day': 60 # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday. 61 return "TO_CHAR(%s, 'D')" % field_name 62 else: 63 return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 60 64 61 65 def date_trunc_sql(self, lookup_type, field_name): 62 66 # Oracle uses TRUNC() for both dates and numbers. … … 216 220 self.connection = Database.connect(conn_string, **self.options) 217 221 cursor = FormatStylePlaceholderCursor(self.connection) 218 222 # Set oracle date to ansi date format. This only needs to execute 219 # once when we create a new connection. 223 # once when we create a new connection. We also set the Territory 224 # to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR(). 220 225 cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD' " 221 "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'") 226 "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF' " 227 "NLS_TERRITORY = 'AMERICA'") 222 228 try: 223 229 self.oracle_version = int(self.connection.version.split('.')[0]) 224 230 # There's no way for the DatabaseOperations class to know the -
tests/modeltests/basic/models.py
68 68 <Article: Area woman programs in Python> 69 69 >>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28) 70 70 <Article: Area woman programs in Python> 71 >>> Article.objects.get(pub_date__week_day=5) 72 <Article: Area woman programs in Python> 71 73 72 74 # The "__exact" lookup type can be omitted, as a shortcut. 73 75 >>> Article.objects.get(id=1) … … 82 84 >>> Article.objects.filter(pub_date__year=2005, pub_date__month=7) 83 85 [<Article: Area woman programs in Python>] 84 86 87 >>> Article.objects.filter(pub_date__week_day=5) 88 [<Article: Area woman programs in Python>] 89 >>> Article.objects.filter(pub_date__week_day=6) 90 [] 91 85 92 # Django raises an Article.DoesNotExist exception for get() if the parameters 86 93 # don't match any object. 87 94 >>> Article.objects.get(id__exact=2) … … 94 101 ... 95 102 DoesNotExist: Article matching query does not exist. 96 103 104 >>> Article.objects.get(pub_date__week_day=6) 105 Traceback (most recent call last): 106 ... 107 DoesNotExist: Article matching query does not exist. 108 97 109 # Lookup by a primary key is the most common case, so Django provides a 98 110 # shortcut for primary-key exact lookups. 99 111 # The following is identical to articles.get(id=1). -
docs/db-api.txt
1534 1534 Note this will match any record with a pub_date on the third day of the month, 1535 1535 such as January 3, July 3, etc. 1536 1536 1537 week_day 1538 ~~~~~~~~ 1539 1540 For date/datetime fields, a 'day of the week' match. 1541 1542 Example:: 1543 1544 Entry.objects.filter(pub_date__week_day=2) 1545 1546 SQL equivalent:: 1547 1548 SELECT ... WHERE EXTRACT('dow' FROM pub_date) = '2'; 1549 1550 (The exact SQL syntax varies for each database engine.) 1551 1552 Note this will match any record with a pub_date that falls on a Tuesday (day 2 1553 of the week), regardless of the month or year in which it occurs. Week days 1554 are zero-indexed, with day 0 being Sunday and day 6 being Saturday. 1555 1537 1556 isnull 1538 1557 ~~~~~~ 1539 1558