2017-05-02 26 views
0

接受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

+0

您的芹菜時區設置爲? –

+0

我發現它,所以我的CELERY_TIMEZONE = TIME_ZONE和我的TIME_ZONE ='America/New_York'在設置 – Radek

+0

http://stackoverflow.com/questions/22786748/celery-scheduled-tasks-problems-with-timezone http:///docs.celeryproject.org/en/latest/userguide/periodic-tasks.html 也許您需要重置數據庫調度程序? –

回答

1

繼評論=)

始終使用django.utils.timezone.now()代替datetime.datetime.now()

請注意,直接在Postgres中,您將看到這些datetime列的格式爲UTC + TZ offset,並且Django會將日期時間轉換爲所需的時區(如果時間在視圖中呈現,則爲服務器TZ或客戶端TZ)。

現在,如果在您的本地Django shell中看到正確的日期時間但不在Heroku中,可能Heroku的列不包含TZ信息(儘管我覺得這很難相信)。我還沒有使用Heroku,但Django遷移將處理timestamp with time zone列。在postgres中,您可以通過psql(或dbshel​​l)檢查列的類型:

\d <your table> 

        Table article 
    Column  |   Type   |  Modifiers 
---------------+---------------------------+--------------------- 
id   | integer     | ... 
pub_date  | timestamp with time zone | ... 
....   | ...      | ... 

希望這會有所幫助。

+0

是的,在heroku它顯示我pub_date |帶時區的時間戳| not null,例如2017-05-03 14:53:00.361049 + 00 - 但是這一次比我需要4小時,本地節目10:53和heroku 14:53,這對我來說很陌生,postgres在本地應該有像heroku postgres相同的功能,所以不知道爲什麼它可以在本地而不是heroku上運行。 – Radek

+0

如果你看到'14:53:00.361049 + 00',這是可以的,因爲NY是UTC-4。 你在哪裏看到14:53?在Heroku?在postgres中?在'shell'中? 'dbshel​​l'?在模型中?日期時間顯示天真而不是tz意識。 –

+0

我通過CLI連接到Postgres到本地,並在桌面上做一個簡單的選擇*以查看所有數據,在本地我看到10:53:00 ....並且在heroku上,我看到了與heroku pg:psql select * - 14:53:00.361049 + 00 - 相同的環境,相同的數據庫爲什麼不同的時間? – Radek