Tivix / django-cron

Write cron business logic as a Python class and let this app do the rest! It enables Django projects to schedule cron tasks, tracks their success / failures, manages contention (via a cache) etc. Basically takes care of all the boring work for you :-)
www.tivix.com
MIT License
900 stars 193 forks source link

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

Closed ukch closed 9 years ago

ukch commented 11 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 9 years ago

Fixed in #48