From b7236b857626660b62800f66700dc887915b96af Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 9 Jan 2010 20:02:41 +0000 Subject: [PATCH] Fixed #12059 - Let TimeField.to_python correctly return a datetime.time object when having a datetime object. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12137 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 2 +- tests/regressiontests/datatypes/models.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 25f44a4d1c7..4763940c81e 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1036,7 +1036,7 @@ class TimeField(Field): # Not usually a good idea to pass in a datetime here (it loses # information), but this can be a side-effect of interacting with a # database backend (e.g. Oracle), so we'll be accommodating. - return value.time + return value.time() # Attempt to parse a datetime: value = smart_str(value) diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py index f6cbc9f0f64..0898b649b10 100644 --- a/tests/regressiontests/datatypes/models.py +++ b/tests/regressiontests/datatypes/models.py @@ -58,6 +58,15 @@ datetime.time(5, 30) >>> d3.consumed_at datetime.datetime(2007, 4, 20, 16, 19, 59) +# Test for ticket #12059: TimeField wrongly handling datetime.datetime object. + +>>> d2.baked_time = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59) +>>> d2.save() + +>>> d3 = Donut.objects.all()[0] +>>> d3.baked_time +datetime.time(16, 19, 59) + # Year boundary tests (ticket #3689) >>> d = Donut(name='Date Test 2007', baked_date=datetime.datetime(year=2007, month=12, day=31), consumed_at=datetime.datetime(year=2007, month=12, day=31, hour=23, minute=59, second=59))