Ticket #7672: dayofweek_filter-r7866.diff

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

Initial Patch - Allow day of week filtering in ORM.

  • 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', 'dow'):
    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', 'dow', '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', 'search'):
     236        if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'dow', 'search'):
    237237            return [value]
    238238        elif lookup_type in ('range', 'in'):
    239239            return value
  • 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 == 'dow':
     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#1017163
    59         return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
     59        if lookup_type == 'dow':
     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.
Back to Top