How would I do the following to get a UTC datetime object, so django doesn't complain about /Library/Python/2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2015-02-11 00:00:00) while time zone support is active.
?
>>> from django.utils import timezone
>>> timezone.date(2014,1,1)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'date'
Taking your example, you should use datetime
and specify the tzinfo argument:
timezone.datetime(2014, 1, 1, tzinfo=timezone.UTC())
You may also be interested in Django's make_aware
function:
https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.timezone.make_aware
Assuming your input is a string with no timezone information, just append a literal "Z"
to your string. This indicates zero offset from UTC time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.