python datetime timezone_python – Django DateTimeField()和timezone.now()

好吧,当我运行功能测试时出现奇怪的时区问题. Django 1.4,

Python 2.7.在MySQL上的DateTimeField()中是否会截断毫秒数?这是我唯一的理论.

模型文件

from django.db import models

from django.utils import timezone

class Search(models.Model):

query = models.CharField(max_length=200, null=True)

query_date = models.DateTimeField(null=True)

test.py

from django.test import TestCase

from django.utils import timezone

from search.models import Search

class SearchModelTest(TestCase):

def test_creating_a_new_search_and_saving_it_to_the_database(self):

# start by creating a new Poll object with its "question" set

search = Search()

search.query = "Test"

search.query_date = timezone.now()

# check we can save it to the database

search.save()

# now check we can find it in the database again

all_search_in_database = Search.objects.all()

self.assertEquals(len(all_search_in_database), 1)

only_search_in_database = all_search_in_database[0]

self.assertEquals(only_search_in_database, search)

# and check that it's saved its two attributes: question and pub_date

self.assertEquals(only_search_in_database.query, "Test")

self.assertEquals(only_search_in_database.query_date, search.query_date)

测试失败了:

self.assertEquals(only_search_in_database.query_date, search.query_date)

AssertionError: datetime.datetime(2013, 1, 16, 21, 12, 35, tzinfo=) != datetime.datetime(2013, 1, 16, 21, 12, 35, 234108, tzinfo=)

我认为发生的事情是在保存到数据库后毫秒被截断.那可能是对的吗?我正在运行MySQL v 5.5. MySQL会截断日期吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值