2017-02-10 123 views
0

我有以下的模型和形式Django的DateTimeField字段和Postgres

class TravelShare(Share): # indirect derived via Share from models.Model 
    source = models.PointField(geography=True, srid=4326) 
    destination = models.PointField(geography=True, srid=4326) 
    departure = models.DateTimeField(default=timezone.now) 
    departure_delta = models.SmallIntegerField(default=60) 

    objects = TravelShareManager() 

    def __str__(self): 
     return self.id 

class TravelShareForm(forms.ModelForm): 

    class Meta: 
     model = TravelShare 
     fields = ['source', 'destination', 'departure', 'departure_delta'] 

現在

def test_TravelShareCreate(self): 

    source = Point(x=48.0, y=11.0, srid=4326) 
    destination = Point(x=48.0001, y=11.0001, srid=4326) 
    now = timezone.localtime(timezone.now()) 
    print ("now : %s" % (now)) 
    now_string = now.strftime('%m.%d.%Y %H:%M:%S') 

    form_data = { 

     'source': source, 
     'destination': destination, 
     'departure': now_string, 
     'departure_delta': 30, 

    } 

    form = TravelShareForm (form_data) 
    print (form.errors) 
    self.assertTrue(form.is_valid()) 

    form.instance.creator = self.creator 
    result = form.save() 
    self.assertEqual(result.creator_id, self.creator.id) 
    self.assertEqual(result.source, source) 
    self.assertEqual(result.destination, destination) 
    result_dep = timezone.localtime(result.departure) 
    print("result_dep : %s" % (result_dep)) 
    self.assertEqual(result.departure, now) 

我結束了測試形式:2017年2月10日15 :49:21.935894 + 01:00

result_dep:2017-10-02 15:49:21 + 02:00

失敗

回溯(最近通話最後一個): 文件 「/home/michael/PycharmProjects/sharadar/main/tests/test_forms.py」,43行,在test_TravelShareCreate self.assertEqual(result.departure ,現在) AssertionError:datet [14個字符] 017,10,2,15,49,21,tzinfo = DstTzInfo'Eur [25個字符] DST)!= datet [14個字符] 017,2,10,15,49 ,21,935894,tzinfo = DstTzI [32個字符] STD

看來這導致日期時間具有不同的TZ或至少顯示+2代替1

感謝您的幫助

邁克爾

+0

我找到了。數據庫的結果取整爲秒,因此15:49.21.93894 + 1.00變爲15:49:21 + 02:00 – mbieren

回答

0

The result from the database is rounded to seconds and therefor 15:49.21.93894+1.00 becomes 15:49:21+02:00

這是完全錯誤的。在將日期時間對象轉換爲字符串now.strftime('%m.%d.%Y %H:%M:%S')期間,您已經放棄了幾微秒的時間。 而且,+02:00是日期時間對象的時區utc偏移量。這不是一個圓整的秒或什麼。

真正的問題在於你的strftime字符串格式。今天,你的時區可能有夏令時。但是,當您將錯誤的格式化日期時間字符串傳遞給表單時,django表單將其解析爲2017-10-02(2017年10月2日),這是您時區中的標準時間。所以,這就是爲什麼你有不同的utc偏移量的原因:+01:00和+02:00。

+0

你說得對。我已經使用strftime('%d。%m。%Y%H:%M:%S')的新測試更新了我的回覆 – mbieren

0

來自數據庫的結果舍入到秒,爲此15:49.21.93894 + 1.00變爲15:49:21 + 02:00

除了以下事實的strftime格式字符串應該('%m。%d。%Y%H:%M:%S'):現在是這樣的:

class TestTravelShareForm(TestCase): 

def setUp(self): 
    self.creator = ShrUser.objects.create_user(username="foo", email="[email protected]", password="bla") 
    self.source = Point(x=48.0, y=11.0, srid=4326) 
    self.destination = Point(x=48.0001, y=11.0001, srid=4326) 
    self.now = timezone.now() 
    self.now_string = self.now.strftime('%d.%m.%Y %H:%M:%S') 

def test_TravelShareCreate(self): 

    form_data = { 

     'source': self.source, 
     'destination': self.destination, 
     'departure': self.now_string, 

    } 

    form = TravelShareForm (form_data) 
    self.assertTrue(form.is_valid()) 

    form.instance.creator = self.creator 
    result = form.save() 

    self.assertEqual(result.creator_id, self.creator.id) 
    self.assertEqual(result.source, self.source) 
    self.assertEqual(result.destination, self.destination) 

    result_dep = result.departure.strftime('%d.%m.%Y %H:%M:%S') 

    self.assertEqual(result_dep, self.now_string) 
相關問題