db "T"与一个有datetime字段的表,例如
CREATE TABLE `exec_job` (
`job_id` int(11) NOT NULL AUTO_INCREMENT,
`status` int(11) NOT NULL,
`create_time` datetime NOT NULL,
`finish_time` datetime NOT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在一台机器上,例如机器IP是192.168.0.100。
我的Django App "M"在另一台机器上,例如:机器IP为192.168.0.200,应用程序M使用数据库t
当我保存或更新数据到db时,我发现"create_time"字段和"finish_time"字段使用192.168.0.200的时间来保存。而,我如何保存或更新datetime字段的datetime与db时间在我的Django应用程序代码?
Models类如下:
class ExecJob(models.Model):
JobId = models.AutoField(db_column = "job_id", primary_key = True)
Status = models.IntegerField(db_column = "status", default = 0)
CreateTime = models.DateTimeField(db_column = "create_time", auto_now_add = True)
FinishTime = models.DateTimeField(db_column = "finish_time", auto_now = True)
class Meta :
db_table = "exec_job"
应用程序代码如下:
jobobj = ExecJob(
#JobId = models.AutoField(db_column = "job_id", primary_key = True)
Status = 0,
#CreateTime = , # be commented
#FinishTime = timezone.now(), # be commented
)
jobobj.save()
您可以在您的模型类
中创建以下方法def save(self, *args, **kwargs):
if not self.id:
self.CreateTime = datetime.datetime.now()
self.FinishTime = datetime.datetime.now()
return super(ExecJob, self).save(*args, **kwargs)