Author: mtredinnick
Date: 2008-08-24 22:17:06 -0500 (Sun, 24 Aug 2008)
New Revision: 8526

Modified:
   django/trunk/django/db/models/fields/__init__.py
   django/trunk/tests/regressiontests/model_regress/models.py
Log:
Fixed #8510 -- Allow both strings (mostly for the admin) and integers to be
used in "month" and "day" filters on date/datetime fields. Without this commit,
SQLite behaved inconsistently after [8494].


Modified: django/trunk/django/db/models/fields/__init__.py
===================================================================
--- django/trunk/django/db/models/fields/__init__.py    2008-08-25 00:32:32 UTC 
(rev 8525)
+++ django/trunk/django/db/models/fields/__init__.py    2008-08-25 03:17:06 UTC 
(rev 8526)
@@ -568,6 +568,13 @@
         else:
             return self.editable or self.auto_now or self.auto_now_add
 
+    def get_db_prep_lookup(self, lookup_type, value):
+        # For "__month" and "__day" lookups, convert the value to a string so
+        # the database backend always sees a consistent type.
+        if lookup_type in ('month', 'day'):
+            return [force_unicode(value)]
+        return super(DateField, self).get_db_prep_lookup(lookup_type, value)
+
     def get_db_prep_value(self, value):
         # Casts dates into the format expected by the backend
         return connection.ops.value_to_db_date(self.to_python(value))

Modified: django/trunk/tests/regressiontests/model_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/model_regress/models.py  2008-08-25 
00:32:32 UTC (rev 8525)
+++ django/trunk/tests/regressiontests/model_regress/models.py  2008-08-25 
03:17:06 UTC (rev 8526)
@@ -57,20 +57,28 @@
 >>> len(a4.article_text)
 5000
 
-# #659 regression test
+# Regression test for #659
 >>> import datetime
 >>> p = Party.objects.create(when = datetime.datetime(1999, 12, 31))
 >>> p = Party.objects.create(when = datetime.datetime(1998, 12, 31))
 >>> p = Party.objects.create(when = datetime.datetime(1999, 1, 1))
->>> [p.when for p in Party.objects.filter(when__month = 2)]
+>>> [p.when for p in Party.objects.filter(when__month=2)]
 []
->>> [p.when for p in Party.objects.filter(when__month = 1)]
+>>> [p.when for p in Party.objects.filter(when__month=1)]
 [datetime.date(1999, 1, 1)]
->>> [p.when for p in Party.objects.filter(when__month = 12)]
+>>> [p.when for p in Party.objects.filter(when__month=12)]
 [datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
->>> [p.when for p in Party.objects.filter(when__year = 1998)]
+>>> [p.when for p in Party.objects.filter(when__year=1998)]
 [datetime.date(1998, 12, 31)]
 
+# Regression test for #8510
+>>> [p.when for p in Party.objects.filter(when__day='31')]
+[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
+>>> [p.when for p in Party.objects.filter(when__month='12')]
+[datetime.date(1999, 12, 31), datetime.date(1998, 12, 31)]
+>>> [p.when for p in Party.objects.filter(when__year='1998')]
+[datetime.date(1998, 12, 31)]
+
 # Check that get_next_by_FIELD and get_previous_by_FIELD don't crash when we
 # have usecs values stored on the database
 #


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to