我想在保存记录时更新相关模型的时间戳。这是我的模特:
class Issue(models.Model):
issueTitle = models.CharField()
issueDescription = models.TextField()
issueCreatedDateTime = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.issueTitle
class IssueHistory(models.Model):
fk_issueID = models.ForeignKey(Issue)
issuehistoryDetail = models.TextField()
issuehistoryCreatedBy = models.ForeignKey(User)
issuehistoryCreatedDateTime = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.fk_issueID
def save(self): #1.1
# Call parent's `save` function
# Record is saved like it would be normally, without the override
super(IssueHistory, self).save() #1.2
#This is where i believe i should be updating the "issueCreatedDateTime" to the same datetime
这个post描述了想要但最后的代码没有发布(除非我误解了)。
为了进一步澄清,这是所需的事件顺序:
我该怎么做?
答案 0 :(得分:1)
def save(self):
super(IssueHistory, self).save() #1.2
# Set Issue issueCreatedDateTime to the same as IssueHistory issueCreatedDateTime
self.fk_issueID.issueCreatedDateTime = self.issuehistoryCreatedDateTime
# Save the Issue
self.fk_issueID.save()