Ticket #7672: dayofweek_filter.r9097.r5.diff
File dayofweek_filter.r9097.r5.diff, 8.5 KB (added by , 16 years ago) |
---|
-
django/db/models/sql/where.py
162 162 params) 163 163 elif lookup_type in ('range', 'year'): 164 164 return ('%s BETWEEN %%s and %%s' % field_sql, params) 165 elif lookup_type in ('month', 'day' ):165 elif lookup_type in ('month', 'day', 'week_day'): 166 166 return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, 167 167 field_sql), params) 168 168 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
196 196 if hasattr(value, 'as_sql'): 197 197 sql, params = value.as_sql() 198 198 return QueryWrapper(('(%s)' % sql), params) 199 if lookup_type in ('regex', 'iregex', 'month', 'day', ' search'):199 if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): 200 200 return [value] 201 201 elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'): 202 202 return [self.get_db_prep_value(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
183 183 dt = util.typecast_timestamp(dt) 184 184 except (ValueError, TypeError): 185 185 return None 186 return unicode(getattr(dt, lookup_type)) 186 if lookup_type == 'week_day': 187 return unicode((dt.isoweekday() % 7) + 1) 188 else: 189 return unicode(getattr(dt, lookup_type)) 187 190 188 191 def _sqlite_date_trunc(lookup_type, dt): 189 192 try: -
django/db/backends/mysql/base.py
115 115 class DatabaseOperations(BaseDatabaseOperations): 116 116 def date_extract_sql(self, lookup_type, field_name): 117 117 # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html 118 return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 118 if lookup_type == 'week_day': 119 # DAYOFWEEK() returns an integer, 1-7, Sunday=1. 120 # Note: WEEKDAY() returns 0-6, Monday=0. 121 return "DAYOFWEEK(%s)" % field_name 122 else: 123 return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name) 119 124 120 125 def date_trunc_sql(self, lookup_type, field_name): 121 126 fields = ['year', 'month', 'day', 'hour', 'minute', 'second'] -
django/db/backends/oracle/base.py
67 67 68 68 def date_extract_sql(self, lookup_type, field_name): 69 69 # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163 70 return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 70 if lookup_type == 'week_day': 71 # TO_CHAR(field, 'D') returns an integer from 1-7, where 1=Sunday. 72 return "TO_CHAR(%s, 'D')" % field_name 73 else: 74 return "EXTRACT(%s FROM %s)" % (lookup_type, field_name) 71 75 72 76 def date_trunc_sql(self, lookup_type, field_name): 73 77 # Oracle uses TRUNC() for both dates and numbers. … … 253 257 self.connection = Database.connect(conn_string, **self.options) 254 258 cursor = FormatStylePlaceholderCursor(self.connection) 255 259 # Set oracle date to ansi date format. This only needs to execute 256 # once when we create a new connection. 260 # once when we create a new connection. We also set the Territory 261 # to 'AMERICA' which forces Sunday to evaluate to a '1' in TO_CHAR(). 257 262 cursor.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD' " 258 "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'") 263 "NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF' " 264 "NLS_TERRITORY = 'AMERICA'") 259 265 try: 260 266 self.oracle_version = int(self.connection.version.split('.')[0]) 261 267 # 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
1168 1168 Note this will match any record with a pub_date on the third day of the month, 1169 1169 such as January 3, July 3, etc. 1170 1170 1171 week_day 1172 ~~~~~~~~ 1173 1174 For date/datetime fields, a 'day of the week' match. 1175 1176 Example:: 1177 1178 Entry.objects.filter(pub_date__week_day=2) 1179 1180 SQL equivalent:: 1181 1182 SELECT ... WHERE EXTRACT('dow' FROM pub_date) = '2'; 1183 1184 (The exact SQL syntax varies for each database engine.) 1185 1186 Note this will match any record with a pub_date that falls on a Monday (day 2 1187 of the week), regardless of the month or year in which it occurs. Week days 1188 are indexed with day 1 being Sunday and day 7 being Saturday. 1189 1171 1190 isnull 1172 1191 ~~~~~~