Ticket #9596: t9596-r9441-date_lookup.diff

File t9596-r9441-date_lookup.diff, 3.8 KB (added by Scott <django@…>, 16 years ago)

patch with test fixed

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

     
    160160                return ('%s IN %s' % (field_sql, extra), params)
    161161            return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(params))),
    162162                    params)
    163         elif lookup_type in ('range', 'year'):
     163        elif lookup_type in ('range', 'year', 'date'):
    164164            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    165165        elif lookup_type in ('month', 'day'):
    166166            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type,
  • 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', 'isnull', 'search', 'regex', 'iregex', 'date',
    88    )])
    99
    1010# Size of each "chunk" for get_iterator calls.
  • django/db/models/fields/__init__.py

     
    212212            return ["%%%s" % connection.ops.prep_for_like_query(value)]
    213213        elif lookup_type == 'isnull':
    214214            return []
     215        elif lookup_type == 'date':
     216            return self.get_db_prep_lookup('range', (datetime.datetime.combine(value, datetime.time.min),
     217                                                     datetime.datetime.combine(value, datetime.time.max)))
    215218        elif lookup_type == 'year':
    216219            try:
    217220                value = int(value)
  • tests/modeltests/lookup/models.py

     
    385385[<Article: bar>, <Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobar>, <Article: foobarbaz>]
    386386"""}
    387387
    388 
    389388if settings.DATABASE_ENGINE != 'mysql':
    390389    __test__['API_TESTS'] += r"""
    391390# grouping and backreferences
    392391>>> Article.objects.filter(headline__regex=r'b(.).*b\1')
    393392[<Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobarbaz>]
    394393"""
     394
     395__test__['API_TESTS'] += r"""
     396# The __date lookup tests for datetime equality without the time
     397>>> for a in Article.objects.all():
     398...     a.delete()
     399...
     400>>> a17 = Article(pub_date=datetime(2008, 11, 14), headline="a17")
     401>>> a17.save()
     402>>> a18 = Article(pub_date=datetime(2008, 11, 14, 23, 59, 59), headline="a18")
     403>>> a18.save()
     404>>> a19 = Article(pub_date=datetime(2008, 11, 15), headline="a19")
     405>>> a19.save()
     406>>> for i in xrange(4):
     407...     print Article.objects.filter(pub_date__date=datetime(2008, 11, (13+i), i))
     408...
     409[]
     410[<Article: a18>, <Article: a17>]
     411[<Article: a19>]
     412[]
     413"""
     414
  • docs/ref/models/querysets.txt

     
    11561156You can use ``range`` anywhere you can use ``BETWEEN`` in SQL -- for dates,
    11571157numbers and even characters.
    11581158
     1159date
     1160~~~~
     1161
     1162Tests datetime fields for date equality, disregarding time.
     1163
     1164Example::
     1165
     1166    yesterday = datetime.date.today() - datetime.timedelta(1)
     1167    Entry.objects.filter(pub_datetime__date = yesterday)
     1168
     1169SQL equivalent::
     1170
     1171    SELECT ... WHERE pub_datetime BETWEEN '2008-11-12 00:00:00' and '2008-11-12 23:59:59';
     1172
    11591173year
    11601174~~~~
    11611175
Back to Top