In django/db/models/fields/init.py file, inside the definition of
DateTimeField.to_python method,
the timezone awareness of a datetime is checked calling the isinstance
function.
{{{
if isinstance(value, datetime.date):
value = datetime.datetime(value.year, value.month, value.day)
if settings.USE_TZ:
# For backwards compatibility, interpret naive datetimes in
# local time. This won't work during DST change, but we can't
# do much about it, so we let the exceptions percolate up the
# call stack.
warnings.warn("DateTimeField %s.%s received a naive datetime "
"(%s) while time zone support is active." %
(self.model.__name__, self.name, value),
RuntimeWarning)
}}}
Is true that, as said here
https://docs.python.org/2.7/library/datetime.html#available-types,
"objects of the date type are always naive", but is wrong to use
isinstance since all datetime.datetime
objects are also instances of datetime.date because datetime.datetime is a
subclass of datetime.date
(which I think is confusing since breakes the OOP intuition that says that
class inheritance should
describe "is a" relationships).
A posible solution is to compare using type(value) == datetime.date or,
more clearly, check using django.utils.is_naive
--
Ticket URL: <https://code.djangoproject.com/ticket/22549>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* easy: 1 => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/22549#comment:1>
* status: new => closed
* resolution: => worksforme
Comment:
Look two lines above -- the complete code is:
{{{
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
# ... stuff you complain about
}}}
If value is a `datetime.datetime` the fist branch returns, you don't hit
the second branch.
--
Ticket URL: <https://code.djangoproject.com/ticket/22549#comment:2>