Ticket #9596: 9596.diff
File 9596.diff, 5.7 KB (added by , 15 years ago) |
---|
-
django/db/models/sql/where.py
### Eclipse Workspace Patch 1.0 #P Django trunk
176 176 return ('%s IN %s' % (field_sql, extra), params) 177 177 return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(params))), 178 178 params) 179 elif lookup_type in ('range', 'year' ):179 elif lookup_type in ('range', 'year', 'date'): 180 180 return ('%s BETWEEN %%s and %%s' % field_sql, params) 181 181 elif lookup_type in ('month', 'day', 'week_day'): 182 182 return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type, field_sql), -
django/db/models/sql/constants.py
4 4 QUERY_TERMS = dict([(x, None) for x in ( 5 5 'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in', 6 6 '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 )]) 9 9 10 10 # Size of each "chunk" for get_iterator calls. 11 11 # Larger values are slightly faster at the expense of more storage space. -
django/db/models/fields/__init__.py
220 220 return ["%%%s" % connection.ops.prep_for_like_query(value)] 221 221 elif lookup_type == 'isnull': 222 222 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))) 223 226 elif lookup_type == 'year': 224 227 try: 225 228 value = int(value) -
tests/modeltests/lookup/models.py
16 16 def __unicode__(self): 17 17 return self.headline 18 18 19 class Comment(models.Model): 20 article = models.ForeignKey(Article) 21 comment = models.TextField() 22 date = models.DateTimeField() 23 19 24 __test__ = {'API_TESTS': r""" 20 25 # Create a couple of Articles. 21 26 >>> from datetime import datetime … … 173 178 >>> Article.objects.extra(select={'id_plus_one': 'id + 1'}).values('id', 'id_plus_two') 174 179 Traceback (most recent call last): 175 180 ... 176 FieldError: Cannot resolve keyword 'id_plus_two' into field. Choices are: headline, id, id_plus_one, pub_date181 FieldError: Cannot resolve keyword 'id_plus_two' into field. Choices are: comment, headline, id, id_plus_one, pub_date 177 182 178 183 # If you don't specify field names to values(), all are returned. 179 184 >>> list(Article.objects.filter(id=5).values()) == [{'id': 5, 'headline': 'Article 5', 'pub_date': datetime(2005, 8, 1, 9, 0)}] … … 293 298 >>> Article.objects.filter(pub_date_year='2005').count() 294 299 Traceback (most recent call last): 295 300 ... 296 FieldError: Cannot resolve keyword 'pub_date_year' into field. Choices are: headline, id, pub_date301 FieldError: Cannot resolve keyword 'pub_date_year' into field. Choices are: comment, headline, id, pub_date 297 302 298 303 >>> Article.objects.filter(headline__starts='Article') 299 304 Traceback (most recent call last): … … 390 395 [<Article: bar>, <Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobar>, <Article: foobarbaz>] 391 396 """ 392 397 393 394 398 if settings.DATABASE_ENGINE != 'mysql': 395 399 __test__['API_TESTS'] += r""" 396 400 # grouping and backreferences 397 401 >>> Article.objects.filter(headline__regex=r'b(.).*b\1') 398 402 [<Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobarbaz>] 399 403 """ 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)) 417 2008-11-13: [] 418 2008-11-14: [<Article: datelookup 2>, <Article: datelookup 1>] 419 2008-11-15: [<Article: datelookup 3>] 420 2008-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
1402 1402 You can use ``range`` anywhere you can use ``BETWEEN`` in SQL -- for dates, 1403 1403 numbers and even characters. 1404 1404 1405 date 1406 ~~~~ 1407 1408 Tests datetime fields for date equality, disregarding time. 1409 1410 Example:: 1411 1412 yesterday = datetime.date.today() - datetime.timedelta(1) 1413 Entry.objects.filter(pub_datetime__date = yesterday) 1414 1415 SQL equivalent:: 1416 1417 SELECT ... WHERE pub_datetime BETWEEN '2008-11-12 00:00:00' AND '2008-11-12 23:59:59'; 1418 1405 1419 year 1406 1420 ~~~~ 1407 1421