diff --git a/src/django-master/comments/models.py b/src/django-master/comments/models.py index 7c3bbc8..b228a4e 100644 --- a/src/django-master/comments/models.py +++ b/src/django-master/comments/models.py @@ -4,14 +4,14 @@ from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from blog.models import Article - +from django.utils import timezone # Create your models here. class Comment(models.Model): body = models.TextField('正文', max_length=300) creation_time = models.DateTimeField(_('creation time'), default=now) - last_modify_time = models.DateTimeField(_('last modify time'), default=now) + last_modify_time = models.DateTimeField(_('last modify time'), default=timezone.now) author = models.ForeignKey( settings.AUTH_USER_MODEL, verbose_name=_('author'), @@ -37,3 +37,9 @@ class Comment(models.Model): def __str__(self): return self.body + + def save(self, *args, **kwargs): + if not self.id: # 如果是新对象,creation_time 将由 default=timezone.now 处理 + self.creation_time = timezone.now() + self.last_modify_time = timezone.now() + super(Comment, self).save(*args, **kwargs)