这是我的表格,我想在此更新end_time
字段
为此,我在更新end_time
字段
07 / May / 2014 07:54:30]“POST / createxml / HTTP / 1.1”500 13328
models.py
class JobDetails(models.Model):
user_id=models.ForeignKey('UserDetails',null=True)
job_name=models.CharField(max_length=45,null=True)
start_time = models.TimeField(_(u"Job Start Time"), auto_now_add=True, blank=True,null=True)
end_time = models.TimeField(_(u"Job End Time"), auto_now_add=True, blank=True,null=True)
status=models.IntegerField(null=True)
job_url=models.URLField(null=True)
def __unicode__(self):
return '%s%s%s%f%s%s' % (self.id,self.job_name,self.start_time,self.end_time,self.status,self.job_url)
Views.py
job_object=JobDetails.objects.get(pk=jobMaxId)
job_object.status=job_status
job_object.end_time=time.localtime(etime)
job_object.save();
此处它成功更新了状态字段,但没有end_time
提供错误
当我使用sys.exc_info()
时,我收到以下错误。
import sys
print sys.exc_info()
(<type 'exceptions.TypeError'>, TypeError('expected string or buffer',), <traceback object at 0xb93c098>)
答案 0 :(得分:3)
您正在使用time
import time
time.localtime(etime)
但是django DateTimeField
和TimeField
支持导入的time
模块
from datetime import time
否则,您将收到错误,因为时间格式不匹配。
您必须先将time
格式更改为日期时间...
from datetime import datetime
from time import mktime, time, localtime
end_time = localtime(etime)
datetime_time = datetime.fromtimestamp(mktime(end_time)) # Convert your time.struct_time to datetime.time
job_object.end_time = datetime_time
job_object.save()