接受Postgres數據庫時區我有一個模型如何使用Django
models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
class Article(models.Model):
sort = models.IntegerField(blank=True)
pub_date = models.DateTimeField(default=timezone.now)
title = models.CharField(max_length=30, blank=True)
common.py
TIME_ZONE = 'America/New_York'
USE_TZ = True
local.py
from .common import *
CELERY_BROKER_URL = env('REDIS_URL')
CELERY_RESULT_BACKEND = env('REDIS_URL')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
production.py
from .common import *
CELERY_BROKER_URL = env('REDIS_URL')
CELERY_RESULT_BACKEND = env('REDIS_URL')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
tasks.py
from __future__ import absolute_import, unicode_literals
from celery.decorators import task
from .models import Article
import urllib2
import json
import datetime
@task(name="articles")
def update_article():
# .... more code
article = Article.objects.get(id=1)
if article != None:
article.pub_date = datetime.datetime.now()
article.title = "Hello"
article.save()
當我在Django的運行shell
import datetime
pub_date = datetime.datetime.now()
print pub_date
的PUB_DATE是EST/'美國/紐約' 時區 - 正確的。
我有芹菜更新文章,我寫了一個簡單的代碼
article.pub_date = datetime.datetime.now()
當PUB_DATE被更新,在DB的日期是UTC並不是美國/紐約/ EST時區,連殼正顯示出我正確等,但與任務運行它,在Postgres的DB是UTC
您的芹菜時區設置爲? –
我發現它,所以我的CELERY_TIMEZONE = TIME_ZONE和我的TIME_ZONE ='America/New_York'在設置 – Radek
http://stackoverflow.com/questions/22786748/celery-scheduled-tasks-problems-with-timezone http:///docs.celeryproject.org/en/latest/userguide/periodic-tasks.html 也許您需要重置數據庫調度程序? –