DateTimeField received a naive datetime
Closed this issue · 4 comments
scottwoodall commented
For starters, amazing utility and just what I was looking for. Thank you for writing and maintaining this project.
If a model has a DateTimeField()
and settings.py
contains USE_TZ=True
then I am receiving the following error:
/home/me/.pythonbrew/venvs/Python-2.7.2/myproject/lib/python2.7/site-packages/
django/db/models/fields/__init__.py:827: RuntimeWarning: DateTimeField
received a naive datetime (2010-12-22 18:30:51.443334) while time zone
support is active. RuntimeWarning)
ValueError: astimezone() cannot be applied to a naive datetime
I've got a workaround by doing the following in generators.py
:
class DateTimeGenerator(Generator):
from django.utils.timezone import now
min_date = now() - datetime.timedelta(365 * 5)
max_date = now() + datetime.timedelta(365 * 1)
The Django version of now()
[1] returns a naive or aware datetime object according to the value of USE_TZ
.
[1] https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-and-aware-datetime-objects
cormac commented
any chance of creating a pull request for this? I've the same issue here
scottwoodall commented
gregmuellegger commented
Fixed via 789d05f
Thanks @scottwoodall for the fix.
cormac commented
nice one @scottwoodall