diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index bc6042ae59..8b000d3c2a 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -411,7 +411,7 @@ class DateField(Field): def get_db_prep_lookup(self, lookup_type, value): if lookup_type == 'range': value = [str(v) for v in value] - elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne'): + elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne') and hasattr(value, 'strftime'): value = value.strftime('%Y-%m-%d') else: value = str(value) diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py index e0c4850ba2..a2c0a14158 100644 --- a/tests/modeltests/lookup/models.py +++ b/tests/modeltests/lookup/models.py @@ -58,6 +58,10 @@ Article 4 >>> Article.objects.filter(headline__startswith='Blah blah').count() 0L +# Date and date/time lookups can also be done with strings. +>>> Article.objects.filter(pub_date__exact='2005-07-27 00:00:00').count() +3L + # in_bulk() takes a list of IDs and returns a dictionary mapping IDs # to objects. >>> Article.objects.in_bulk([1, 2])