Ticket #8424: time_filters.diff

File time_filters.diff, 4.5 KB (added by Vincent Foley, 16 years ago)
  • django/db/models/sql/where.py

     
    162162                    params)
    163163        elif lookup_type in ('range', 'year'):
    164164            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    165         elif lookup_type in ('month', 'day'):
     165        elif lookup_type in ('month', 'day', 'hour', 'minute', 'second'):
    166166            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type,
    167167                    field_sql), params)
    168168        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', 'hour', 'minute', 'second', 'isnull', 'search', 'regex',
     8    'iregex',
    89    )])
    910
    1011# Size of each "chunk" for get_iterator calls.
  • django/db/models/fields/__init__.py

     
    208208        if hasattr(value, 'as_sql'):
    209209            sql, params = value.as_sql()
    210210            return QueryWrapper(('(%s)' % sql), params)
    211         if lookup_type in ('regex', 'iregex', 'month', 'day', 'search'):
     211        if lookup_type in ('regex', 'iregex', 'month', 'day', 'hour',
     212                           'minute', 'second', 'search'):
    212213            return [value]
    213214        elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
    214215            return [self.get_db_prep_value(value)]
  • tests/modeltests/basic/models.py

     
    7575>>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
    7676<Article: Area woman programs in Python>
    7777
     78
    7879# The "__exact" lookup type can be omitted, as a shortcut.
    7980>>> Article.objects.get(id=1)
    8081<Article: Area woman programs in Python>
     
    8889>>> Article.objects.filter(pub_date__year=2005, pub_date__month=7)
    8990[<Article: Area woman programs in Python>]
    9091
     92>>> Article.objects.filter(pub_date__hour=0)
     93[<Article: Area woman programs in Python>]
     94>>> Article.objects.filter(pub_date__hour=1)
     95[]
     96
     97>>> Article.objects.filter(pub_date__minute=0)
     98[<Article: Area woman programs in Python>]
     99>>> Article.objects.filter(pub_date__minute=1)
     100[]
     101
     102>>> Article.objects.filter(pub_date__second=0)
     103[<Article: Area woman programs in Python>]
     104>>> Article.objects.filter(pub_date__second=1)
     105[]
     106
    91107# Django raises an Article.DoesNotExist exception for get() if the parameters
    92108# don't match any object.
    93109>>> Article.objects.get(id__exact=2)
  • docs/db-api.txt

     
    15801580Note this will match any record with a pub_date on the third day of the month,
    15811581such as January 3, July 3, etc.
    15821582
     1583hour
     1584~~~~
     1585
     1586For date/datetime fields, exact hour match.
     1587
     1588Example::
     1589
     1590    Entry.objects.filter(pub_date__hour=3)
     1591
     1592SQL equivalent::
     1593
     1594    SELECT ... WHERE EXTRACT('hour' FROM pub_date) = '3';
     1595
     1596(The exact SQL syntax varies for each database engine.)
     1597
     1598This will match every record with a pub_date that occurred between
     15993:00 AM and 3:59 AM.
     1600
     1601minute
     1602~~~~
     1603
     1604For date/datetime fields, exact minute match.
     1605
     1606Example::
     1607
     1608    Entry.objects.filter(pub_date__minute=30)
     1609
     1610SQL equivalent::
     1611
     1612    SELECT ... WHERE EXTRACT('minute' FROM pub_date) = '30';
     1613
     1614(The exact SQL syntax varies for each database engine.)
     1615
     1616This will match every record with a pub_date that occurred at the 30th
     1617minute of any hour, such as 8:30 AM or 11:30 PM.
     1618
     1619second
     1620~~~~
     1621
     1622For date/datetime fields, exact second match.
     1623
     1624Example::
     1625
     1626    Entry.objects.filter(pub_date__second=0)
     1627
     1628SQL equivalent::
     1629
     1630    SELECT ... WHERE EXTRACT('second' FROM pub_date) = '0';
     1631
     1632(The exact SQL syntax varies for each database engine.)
     1633
     1634This will match every record with a pub_date that occurred at the 0th
     1635second of any minute.
     1636
    15831637isnull
    15841638~~~~~~
    15851639
Back to Top