Ticket #8424: 8424-time-filters.diff

File 8424-time-filters.diff, 9.2 KB (added by Timothée Peignier, 14 years ago)

Updated patch

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index fd0a295..8b02bff 100644
    a b class Field(object):  
    283283            return value._prepare()
    284284
    285285        if lookup_type in (
    286                 'regex', 'iregex', 'month', 'day', 'week_day', 'search',
    287                 'contains', 'icontains', 'iexact', 'startswith', 'istartswith',
     286                'month', 'day', 'week_day', 'hour', 'minute', 'second',
     287                'regex', 'iregex', 'search', 'contains', 'icontains',
     288                'iexact', 'startswith', 'istartswith',
    288289                'endswith', 'iendswith', 'isnull'
    289290            ):
    290291            return value
    class Field(object):  
    317318                sql, params = value._as_sql(connection=connection)
    318319            return QueryWrapper(('(%s)' % sql), params)
    319320
    320         if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'):
     321        if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'hour', 'minute', 'second', 'search'):
    321322            return [value]
    322323        elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):
    323324            return [self.get_db_prep_value(value, connection=connection, prepared=prepared)]
    class DateField(Field):  
    639640    def get_prep_lookup(self, lookup_type, value):
    640641        # For "__month", "__day", and "__week_day" lookups, convert the value
    641642        # to an int so the database backend always sees a consistent type.
    642         if lookup_type in ('month', 'day', 'week_day'):
     643        if lookup_type in ('month', 'day', 'week_day', 'hour', 'minute', 'second'):
    643644            return int(value)
    644645        return super(DateField, self).get_prep_lookup(lookup_type, value)
    645646
  • django/db/models/sql/constants.py

    diff --git a/django/db/models/sql/constants.py b/django/db/models/sql/constants.py
    index 63c704f..95b244d 100644
    a b import re  
    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', 'week_day', 'isnull', 'search', 'regex', 'iregex',
     7    'month', 'day', 'week_day', 'hour', 'minute', 'second',
     8    'isnull', 'search', 'regex', 'iregex'
    89    )])
    910
    1011# Size of each "chunk" for get_iterator calls.
  • django/db/models/sql/where.py

    diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
    index 2427a52..62481f6 100644
    a b class WhereNode(tree.Node):  
    199199                        params)
    200200        elif lookup_type in ('range', 'year'):
    201201            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    202         elif lookup_type in ('month', 'day', 'week_day'):
     202        elif lookup_type in ('month', 'day', 'week_day', 'hour', 'minute', 'second'):
    203203            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql),
    204204                    params)
    205205        elif lookup_type == 'isnull':
  • docs/ref/models/querysets.txt

    diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
    index 8bff953..948fd5b 100644
    a b Note this will match any record with a pub_date that falls on a Monday (day 2  
    16461646of the week), regardless of the month or year in which it occurs. Week days
    16471647are indexed with day 1 being Sunday and day 7 being Saturday.
    16481648
     1649.. fieldlookup:: hour
     1650
     1651hour
     1652~~~~
     1653
     1654.. versionadded:: 1.3
     1655
     1656For date/datetime fields, exact hour match.
     1657
     1658Example::
     1659
     1660    Entry.objects.filter(pub_date__hour=3)
     1661
     1662SQL equivalent::
     1663
     1664    SELECT ... WHERE EXTRACT('hour' FROM pub_date) = '3';
     1665
     1666(The exact SQL syntax varies for each database engine.)
     1667
     1668This will match every record with a pub_date that occurred between
     16693:00 AM and 3:59 AM.
     1670
     1671.. fieldlookup:: minute
     1672
     1673minute
     1674~~~~~~
     1675
     1676.. versionadded:: 1.3
     1677
     1678For date/datetime fields, exact minute match.
     1679
     1680Example::
     1681
     1682    Entry.objects.filter(pub_date__minute=30)
     1683
     1684SQL equivalent::
     1685
     1686    SELECT ... WHERE EXTRACT('minute' FROM pub_date) = '30';
     1687
     1688(The exact SQL syntax varies for each database engine.)
     1689
     1690This will match every record with a pub_date that occurred at the 30th
     1691minute of any hour, such as 8:30 AM or 11:30 PM.
     1692
     1693.. fieldlookup:: second
     1694
     1695second
     1696~~~~~~
     1697
     1698.. versionadded:: 1.3
     1699
     1700For date/datetime fields, exact second match.
     1701
     1702Example::
     1703
     1704    Entry.objects.filter(pub_date__second=0)
     1705
     1706SQL equivalent::
     1707
     1708    SELECT ... WHERE EXTRACT('second' FROM pub_date) = '0';
     1709
     1710(The exact SQL syntax varies for each database engine.)
     1711
     1712This will match every record with a pub_date that occurred at the 0th
     1713second of any minute.
     1714
    16491715.. fieldlookup:: isnull
    16501716
    16511717isnull
  • tests/modeltests/basic/tests.py

    diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py
    index 966798d..af8e965 100644
    a b class ModelTest(TestCase):  
    1818        a = Article(
    1919            id=None,
    2020            headline='Area man programs in Python',
    21             pub_date=datetime(2005, 7, 28),
     21            pub_date=datetime(2005, 7, 28, 9, 23, 34),
    2222        )
    2323
    2424        # Save it into the database. You have to call save() explicitly.
    class ModelTest(TestCase):  
    3333
    3434        # Access database columns via Python attributes.
    3535        self.assertEqual(a.headline, 'Area man programs in Python')
    36         self.assertEqual(a.pub_date, datetime(2005, 7, 28, 0, 0))
     36        self.assertEqual(a.pub_date, datetime(2005, 7, 28, 9, 23, 34))
    3737
    3838        # Change values by changing the attributes, then calling save().
    3939        a.headline = 'Area woman programs in Python'
    class ModelTest(TestCase):  
    5050        self.assertEqual(Article.objects.get(pub_date__year=2005, pub_date__month=7), a)
    5151        self.assertEqual(Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28), a)
    5252        self.assertEqual(Article.objects.get(pub_date__week_day=5), a)
     53        self.assertEqual(Article.objects.get(pub_date__hour=9, pub_date__minute=23), a)
     54        self.assertEqual(Article.objects.get(pub_date__hour=9), a)
     55        self.assertEqual(Article.objects.get(pub_date__minute=23), a)
     56        self.assertEqual(Article.objects.get(pub_date__second=34), a)
    5357
    5458        # The "__exact" lookup type can be omitted, as a shortcut.
    5559        self.assertEqual(Article.objects.get(id=a.id), a)
    class ModelTest(TestCase):  
    7680            Article.objects.filter(pub_date__week_day=6),
    7781            [],
    7882        )
     83        self.assertQuerysetEqual(
     84            Article.objects.filter(pub_date__hour=9),
     85            ['<Article: Area woman programs in Python>'],
     86        )
     87        self.assertQuerysetEqual(
     88            Article.objects.filter(pub_date__minute=23),
     89            ['<Article: Area woman programs in Python>'],
     90        )
     91        self.assertQuerysetEqual(
     92            Article.objects.filter(pub_date__month=7, pub_date__hour=8),
     93            [],
     94        )
     95        self.assertQuerysetEqual(
     96            Article.objects.filter(pub_date__month=7, pub_date__second=27),
     97            [],
     98        )
    7999
    80100        # Django raises an Article.DoesNotExist exception for get() if the
    81101        # parameters don't match any object.
    class ModelTest(TestCase):  
    101121            pub_date__week_day=6,
    102122        )
    103123
     124        self.assertRaisesRegexp(
     125            ObjectDoesNotExist,
     126            "Article matching query does not exist.",
     127            Article.objects.get,
     128            pub_date__second=30,
     129        )
     130
    104131        # Lookup by a primary key is the most common case, so Django
    105132        # provides a shortcut for primary-key exact lookups.
    106133        # The following is identical to articles.get(id=a.id).
  • tests/regressiontests/null_queries/models.py

    diff --git a/tests/regressiontests/null_queries/models.py b/tests/regressiontests/null_queries/models.py
    index 442535c..adcb0bd 100644
    a b class OuterB(models.Model):  
    2222
    2323class Inner(models.Model):
    2424    first = models.ForeignKey(OuterA)
    25     second = models.ForeignKey(OuterB, null=True)
     25    secondary = models.ForeignKey(OuterB, null=True)
  • tests/regressiontests/null_queries/tests.py

    diff --git a/tests/regressiontests/null_queries/tests.py b/tests/regressiontests/null_queries/tests.py
    index 72dcd51..edc0cc6 100644
    a b class NullQueriesTests(TestCase):  
    5353        """
    5454        obj = OuterA.objects.create()
    5555        self.assertQuerysetEqual(
    56             OuterA.objects.filter(inner__second=None),
     56            OuterA.objects.filter(inner__secondary=None),
    5757            ['<OuterA: OuterA object>']
    5858        )
    5959        self.assertQuerysetEqual(
    60             OuterA.objects.filter(inner__second__data=None),
     60            OuterA.objects.filter(inner__secondary__data=None),
    6161            ['<OuterA: OuterA object>']
    6262        )
    6363
    6464        inner_obj = Inner.objects.create(first=obj)
    6565        self.assertQuerysetEqual(
    66             Inner.objects.filter(first__inner__second=None),
     66            Inner.objects.filter(first__inner__secondary=None),
    6767            ['<Inner: Inner object>']
    6868        )
    6969
Back to Top