Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeField received a naive datetime while time zone support is active. #23

Closed
ukch opened this issue Aug 22, 2013 · 3 comments
Closed

Comments

@ukch
Copy link

ukch commented Aug 22, 2013

The above warning occurs when running manage.py runcrons on Django 1.4. The complete traceback is:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/joel/.virtualenvs/ecomarket/local/lib/python2.7/site-packages/django_cron/management/commands/runcrons.py", line 57, in handle
    silent=options['silent'])
  File "/home/joel/.virtualenvs/ecomarket/local/lib/python2.7/site-packages/django_cron/management/commands/runcrons.py", line 74, in run_cron_with_cache_check
    CronJobManager.run(instance, force)
  File "/home/joel/.virtualenvs/ecomarket/local/lib/python2.7/site-packages/django_cron/__init__.py", line 91, in run
    if CronJobManager.__should_run_now(cron_job, force):
  File "/home/joel/.virtualenvs/ecomarket/local/lib/python2.7/site-packages/django_cron/__init__.py", line 78, in __should_run_now
    qset = CronJobLog.objects.filter(code=cron_job.code, start_time__gt=datetime.today().date(), ran_at_time=time_data)
  File "/usr/lib/python2.7/dist-packages/django/db/models/manager.py", line 143, in filter
    return self.get_query_set().filter(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 624, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 642, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1250, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 1185, in add_filter
    connector)
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/where.py", line 69, in add
    value = obj.prepare(lookup_type, value)
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/where.py", line 320, in prepare
    return self.field.get_prep_lookup(lookup_type, value)
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 709, in get_prep_lookup
    return super(DateField, self).get_prep_lookup(lookup_type, value)
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 310, in get_prep_lookup
    return self.get_prep_value(value)
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 801, in get_prep_value
    value = self.to_python(value)
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 761, in to_python
    RuntimeWarning)
RuntimeWarning: DateTimeField received a naive datetime (2013-08-22 00:00:00) while time zone support is active.

This comes from the fact that you are passing a datetime.date object to a DateTimeField, which is unsupported. A workaround is to use a timezone-aware datetime object (eg. by using django.utils.timezone.now) and setting the time to midnight.

Although this is only a warning, it is causing a large amount of email for us as we have runcrons set up to run every five minutes and email us any output from stderr.

@karolis-sh
Copy link

Also appears in Django 1.6.5:

/.virtualenvs/my-env/local/lib/python2.7/site-packages/django/db/models/fields/init.py:848: RuntimeWarning: DateTimeField CronJobLog.start_time received a naive datetime (2014-09-11 00:00:00) while time zone support is active.
RuntimeWarning)

@martinmacko47
Copy link

until it's fixed, you can suppress the warning with:

import warnings
warnings.filterwarnings(u'ignore',
        message=u'DateTimeField CronJobLog.start_time received a naive datetime',
        category=RuntimeWarning,
        )

@maciej-jaworski
Copy link
Contributor

Fixed in #48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants