Ticket #9596: dj_date_lookup_c_3.patch

File dj_date_lookup_c_3.patch, 3.7 KB (added by Scott <django@…>, 13 years ago)

patch c_3 with tests

  • 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

     
    383383[<Article: barfoobaz>, <Article: baz>, <Article: bazbaRFOO>, <Article: foobarbaz>, <Article: foobaz>]
    384384>>> Article.objects.filter(headline__iregex=r'b.*ar')
    385385[<Article: bar>, <Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobar>, <Article: foobarbaz>]
     386
     387# The __date lookup tests for datetime equality without the time
     388>>> for a in Article.objects.all():
     389...     a.delete()
     390...
     391>>> a17 = Article(pub_date=datetime(2008, 11, 14), headline="a17")
     392>>> a17.save()
     393>>> a18 = Article(pub_date=datetime(2008, 11, 14, 23, 59, 59), headline="a18")
     394>>> a18.save()
     395>>> a19 = Article(pub_date=datetime(2008, 11, 15), headline="a19")
     396>>> a19.save()
     397>>> for i in xrange(4):
     398...     print Article.objects.filter(pub_date__date=datetime(2008, 11, (13+i), i))
     399...
     400[]
     401[<Article: a18>, <Article: a17>]
     402[<Article: a19>]
     403[]
    386404"""}
    387405
    388406
  • 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