好吧,当我运行功能测试时出现奇怪的时区问题. 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会截断日期吗?