Ticket #9596: t9596-r9441-date_lookup.2.diff

File t9596-r9441-date_lookup.2.diff, 3.9 KB (added by Scott, 15 years ago)

Updated patch

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

     
    176176                return ('%s IN %s' % (field_sql, extra), params)
    177177            return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(params))),
    178178                    params)
    179         elif lookup_type in ('range', 'year'):
     179        elif lookup_type in ('range', 'year', 'date'):
    180180            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    181181        elif lookup_type in ('month', 'day', 'week_day'):
    182182            return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql),
  • 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', 'week_day', 'isnull', 'search', 'regex', 'iregex',
    8     )])
     7    'month', 'day', 'week_day', 'date', 'isnull', 'search', 'regex', 'iregex',
     8)])
    99
    1010# Size of each "chunk" for get_iterator calls.
    1111# Larger values are slightly faster at the expense of more storage space.
  • django/db/models/fields/__init__.py

     
    220220            return ["%%%s" % connection.ops.prep_for_like_query(value)]
    221221        elif lookup_type == 'isnull':
    222222            return []
     223        elif lookup_type == 'date':
     224            return self.get_db_prep_lookup('range', (datetime.datetime.combine(value, datetime.time.min),
     225                                                     datetime.datetime.combine(value, datetime.time.max)))
    223226        elif lookup_type == 'year':
    224227            try:
    225228                value = int(value)
  • tests/modeltests/lookup/models.py

     
    390390[<Article: bar>, <Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobar>, <Article: foobarbaz>]
    391391"""
    392392
    393 
    394393if settings.DATABASE_ENGINE != 'mysql':
    395394    __test__['API_TESTS'] += r"""
    396395# grouping and backreferences
    397396>>> Article.objects.filter(headline__regex=r'b(.).*b\1')
    398397[<Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobarbaz>]
    399398"""
     399
     400__test__['API_TESTS'] += r"""
     401# The __date lookup tests for datetime equality without the time
     402>>> for a in Article.objects.all():
     403...     a.delete()
     404...
     405>>> a17 = Article(pub_date=datetime(2008, 11, 14), headline="a17")
     406>>> a17.save()
     407>>> a18 = Article(pub_date=datetime(2008, 11, 14, 23, 59, 59), headline="a18")
     408>>> a18.save()
     409>>> a19 = Article(pub_date=datetime(2008, 11, 15), headline="a19")
     410>>> a19.save()
     411>>> for i in xrange(4):
     412...     print Article.objects.filter(pub_date__date=datetime(2008, 11, (13+i), i))
     413...
     414[]
     415[<Article: a18>, <Article: a17>]
     416[<Article: a19>]
     417[]
     418"""
     419
  • docs/ref/models/querysets.txt

     
    12951295You can use ``range`` anywhere you can use ``BETWEEN`` in SQL -- for dates,
    12961296numbers and even characters.
    12971297
     1298date
     1299~~~~
     1300
     1301Tests datetime fields for date equality, disregarding time.
     1302
     1303Example::
     1304
     1305    yesterday = datetime.date.today() - datetime.timedelta(1)
     1306    Entry.objects.filter(pub_datetime__date = yesterday)
     1307
     1308SQL equivalent::
     1309
     1310    SELECT ... WHERE pub_datetime BETWEEN '2008-11-12 00:00:00' AND '2008-11-12 23:59:59';
     1311
    12981312year
    12991313~~~~
    13001314
Back to Top