4

In short, I'm trying to show the user a DateTime field initialized with a timezone aware datetime object, allow them to edit it and post it back. Without doing anything special I get a warning about a non-timezone aware datetime being returned to the form's DateTime field.

/django/db/models/fields/__init__.py:1474: RuntimeWarning:
    DateTimeField MyModel.datetimefield received a naive datetime (2015-09-09 15:55:00)
    while time zone support is active.

I started by reading the django time zones docs. I installed pytz (pip install pytz) and made sure USE_TZ = True was set.

I'm using django-easy-timezones, which sets the "current" timezone based on the requester's IP with a call to timezone.activate(). As I understand it, the setting TIME_ZONE would otherwise be used as the default. I'm also also using django-bootstrap3-datetimepicker for a nice widget in my form.

The timezone doesn't appear to be sent with the initial form data. Instead, the "current" timezone is used to create and send a naive local datetime. From this I gather the clean_[field] method is meant to assume what comes back in the POST is also in the "current" timezone. Is this done automatically or am I meant to make the data timezone aware myself? If so, why might I be getting the warning?

Would it make more sense to simply send the timezone back and forth, always having a timezone aware datetime? If so, getting the datetime to javascript is discussed here, but I haven't found a nice way of returning it to the DateTime field format wise (especially with the widget not using a format with a timezone).

Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180

0 Answers0