我在检索datetime列时遇到问题 . model.field返回None,而不是返回正确的日期时间 .
CustomerPeriod模型定义如下:
class CustomerPeriod(models.Model):
id = models.DecimalField(primary_key=True, decimal_places=0, max_digits=22)
customer_id = models.ForeignKey(Customer)
begin_date = models.DateTimeField()
end_date = models.DateTimeField()
例如,该表填充如下:
customer_period = CustomerPeriod()
customer_period.id = Decimal('1118632')
customer_period.customer_id = 1
customer_period.begin_date = datetime.datetime(2008, 8, 15, 5, 0)
customer_period.end_date = datetime.datetime(2008, 8, 15, 6, 0)
customer_period.save()
现在,尝试获取begin_date字段的值,返回null而不是上面填充的预期日期 .
CustomerPeriod.objects.all()[0].begin_date
返回无???!
另外,我可以在数据库浏览器中看到预期的数据 .
在此先感谢您的帮助 .
版本:
-
Sqlite3 3.7.12
-
Python 2.7.4
-
Django 1.5.4
斯图尔特
1 回答
不确定这是否还有待处理,但我自己也只是处理了这个问题 . 您需要确保sqlite db的列类型是datetime . 如果它只是日期而不是当你尝试用python将其拉出时返回None . 删除该表,如果是,则重新同步 .