Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#22549 closed Bug (worksforme)

DateTimeField raises timezone awareness related RuntimeWarnings when it should'nt

Reported by: matias@… Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: Normal Keywords:
Cc: matias@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I've experienced some issues with wrong RuntimeWarnings about naive datetimes being raised when the
datetime I was using was perfectly timezone aware and I think I found the reason why:

In django/db/models/fields/ file, inside the definition of DateTimeField.to_python method,
the timezone awareness of a datetime is checked calling the isinstance function.

if isinstance(value,
    value = datetime.datetime(value.year, value.month,
    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__,, value),

Is true that, as said here,
"objects of the date type are always naive", but is wrong to use isinstance since all datetime.datetime
objects are also instances of because datetime.datetime is a subclass of
(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) == or, more clearly, check using django.utils.is_naive

Change History (2)

comment:1 Changed 4 years ago by Ramiro Morales

Easy pickings: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 4 years ago by Aymeric Augustin

Resolution: worksforme
Status: newclosed

Look two lines above -- the complete code is:

        if isinstance(value, datetime.datetime):
            return value
        if isinstance(value,
            # ... stuff you complain about

If value is a datetime.datetime the first branch returns, you don't hit the second branch.

Last edited 4 years ago by Aymeric Augustin (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top