Ticket #9596: 9596.diff

File 9596.diff, 5.7 KB (added by Chris Beaven, 15 years ago)

new patch (with failing test, showing backwards incompatibility)

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

    ### Eclipse Workspace Patch 1.0
    #P Django trunk
     
    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

     
    1616    def __unicode__(self):
    1717        return self.headline
    1818
     19class Comment(models.Model):
     20    article = models.ForeignKey(Article)
     21    comment = models.TextField()
     22    date = models.DateTimeField()
     23
    1924__test__ = {'API_TESTS': r"""
    2025# Create a couple of Articles.
    2126>>> from datetime import datetime
     
    173178>>> Article.objects.extra(select={'id_plus_one': 'id + 1'}).values('id', 'id_plus_two')
    174179Traceback (most recent call last):
    175180    ...
    176 FieldError: Cannot resolve keyword 'id_plus_two' into field. Choices are: headline, id, id_plus_one, pub_date
     181FieldError: Cannot resolve keyword 'id_plus_two' into field. Choices are: comment, headline, id, id_plus_one, pub_date
    177182
    178183# If you don't specify field names to values(), all are returned.
    179184>>> list(Article.objects.filter(id=5).values()) == [{'id': 5, 'headline': 'Article 5', 'pub_date': datetime(2005, 8, 1, 9, 0)}]
     
    293298>>> Article.objects.filter(pub_date_year='2005').count()
    294299Traceback (most recent call last):
    295300    ...
    296 FieldError: Cannot resolve keyword 'pub_date_year' into field. Choices are: headline, id, pub_date
     301FieldError: Cannot resolve keyword 'pub_date_year' into field. Choices are: comment, headline, id, pub_date
    297302
    298303>>> Article.objects.filter(headline__starts='Article')
    299304Traceback (most recent call last):
     
    390395[<Article: bar>, <Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobar>, <Article: foobarbaz>]
    391396"""
    392397
    393 
    394398if settings.DATABASE_ENGINE != 'mysql':
    395399    __test__['API_TESTS'] += r"""
    396400# grouping and backreferences
    397401>>> Article.objects.filter(headline__regex=r'b(.).*b\1')
    398402[<Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobarbaz>]
    399403"""
     404
     405__test__['API_TESTS'] += r"""
     406# The __date lookup tests for datetime equality without the time
     407>>> def create_datelookup_article(title, d):
     408...     Article.objects.create(pub_date=d, headline="datelookup %s" % title)
     409>>> create_datelookup_article(1, datetime(2008, 11, 14))
     410>>> create_datelookup_article(2, datetime(2008, 11, 14, 23, 59, 59))
     411>>> create_datelookup_article(3, datetime(2008, 11, 15))
     412>>> articles = Article.objects.filter(headline__startswith='datelookup')
     413
     414>>> for i in xrange(4):
     415...     day = datetime(2008, 11, (13+i), i)
     416...     print '%s: %s' % (day.date(), articles.filter(pub_date__date=day))
     4172008-11-13: []
     4182008-11-14: [<Article: datelookup 2>, <Article: datelookup 1>]
     4192008-11-15: [<Article: datelookup 3>]
     4202008-11-16: []
     421
     422# Test that the __date lookup doesn't swallow date fields.
     423>>> comment = articles[0].comment_set.create(comment='test', date=datetime(2008, 11, 16))
     424>>> print articles.filter(comment__date=datetime(2008, 11, 16))
     425[<Article: datelookup 3>]
     426"""
     427
  • docs/ref/models/querysets.txt

     
    14021402You can use ``range`` anywhere you can use ``BETWEEN`` in SQL -- for dates,
    14031403numbers and even characters.
    14041404
     1405date
     1406~~~~
     1407
     1408Tests datetime fields for date equality, disregarding time.
     1409
     1410Example::
     1411
     1412    yesterday = datetime.date.today() - datetime.timedelta(1)
     1413    Entry.objects.filter(pub_datetime__date = yesterday)
     1414
     1415SQL equivalent::
     1416
     1417    SELECT ... WHERE pub_datetime BETWEEN '2008-11-12 00:00:00' AND '2008-11-12 23:59:59';
     1418
    14051419year
    14061420~~~~
    14071421
Back to Top