Ticket #7672: dayofweek_filter-r7866.v2.diff

File dayofweek_filter-r7866.v2.diff, 6.8 KB (added by rossp, 7 years ago)

New revision, see note below.

  • django/db/models/sql/where.py

     
    152152                    params)
    153153        elif lookup_type in ('range', 'year'):
    154154            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'):
    156156            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type,
    157157                    field_sql), params)
    158158        elif lookup_type == 'isnull':
  • django/db/models/sql/constants.py

     
    44QUERY_TERMS = dict([(x, None) for x in (
    55    'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in',
    66    'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year',
    7     'month', 'day', 'isnull', 'search', 'regex', 'iregex',
     7    'month', 'day', 'week_day', 'isnull', 'search', 'regex', 'iregex',
    88    )])
    99
    1010# Size of each "chunk" for get_iterator calls.
  • django/db/models/fields/__init__.py

     
    233233        if hasattr(value, 'as_sql'):
    234234            sql, params = value.as_sql()
    235235            return QueryWrapper(('(%s)' % sql), params)
    236         if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'sea                                   rch'):
     236        if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'wee                                   k_day', 'search'):
    237237            return [value]
    238238        elif lookup_type in ('range', 'in'):
    239239            return value
  • django/db/backends/postgresql/operations.py

     
    2626
    2727    def date_extract_sql(self, lookup_type, field_name):
    2828        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRAC                                   T
    29         return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
     29               if lookup_type == 'week_day':
     30                       return "EXTRACT('dow' FROM %s)" % field_name
     31               else:
     32                       return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
    3033
    3134    def date_trunc_sql(self, lookup_type, field_name):
    3235        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  • django/db/backends/mysql/base.py

     
    6868class DatabaseOperations(BaseDatabaseOperations):
    6969    def date_extract_sql(self, lookup_type, field_name):
    7070        # 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            return "DAYOFWEEK(%s)" % field_name
     73        else:
     74            return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
    7275
    7376    def date_trunc_sql(self, lookup_type, field_name):
    7477        fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
  • django/db/backends/oracle/base.py

     
    5656
    5757    def date_extract_sql(self, lookup_type, field_name):
    5858        # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#101716                                   3
    59         return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
     59        if lookup_type == 'week_day':
     60            return "TO_CHAR(%s, 'D')" % field_name
     61        else:
     62            return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
    6063
    6164    def date_trunc_sql(self, lookup_type, field_name):
    6265        # Oracle uses TRUNC() for both dates and numbers.
  • tests/modeltests/basic/models.py

     
    6868<Article: Area woman programs in Python>
    6969>>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
    7070<Article: Area woman programs in Python>
     71>>> Article.objects.get(pub_date__week_day=5)
     72<Article: Area woman programs in Python>
    7173
    7274# The "__exact" lookup type can be omitted, as a shortcut.
    7375>>> Article.objects.get(id=1)
     
    8284>>> Article.objects.filter(pub_date__year=2005, pub_date__month=7)
    8385[<Article: Area woman programs in Python>]
    8486
     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
    8592# Django raises an Article.DoesNotExist exception for get() if the parameters
    8693# don't match any object.
    8794>>> Article.objects.get(id__exact=2)
     
    94101    ...
    95102DoesNotExist: Article matching query does not exist.
    96103
     104>>> Article.objects.get(pub_date__week_day=6)
     105Traceback (most recent call last):
     106    ...
     107DoesNotExist: Article matching query does not exist.
     108
    97109# Lookup by a primary key is the most common case, so Django provides a
    98110# shortcut for primary-key exact lookups.
    99111# The following is identical to articles.get(id=1).
  • docs/db-api.txt

     
    15341534Note this will match any record with a pub_date on the third day of the month,
    15351535such as January 3, July 3, etc.
    15361536
     1537week_day
     1538~~~~~~~~
     1539
     1540For date/datetime fields, a 'day of the week' match.
     1541
     1542Example::
     1543
     1544    Entry.objects.filter(pub_date__week_day=2)
     1545
     1546SQL equivalent::
     1547
     1548    SELECT ... WHERE EXTRACT('dow' FROM pub_date) = '2';
     1549
     1550(The exact SQL syntax varies for each database engine.)
     1551
     1552Note this will match any record with a pub_date that falls on a Tuesday (day 2
     1553of the week), regardless of the month or year in which it occurs. Week days
     1554are zero-indexed, with day 0 being Sunday and day 6 being Saturday.
     1555
    15371556isnull
    15381557~~~~~~
    15391558
Back to Top