有没有一种方法可以保持在updateview中的时间与Django中createview中的时间相同

时间:2019-03-03 07:35:42

标签: python django

我意识到当我运行updateview时,listdate更改为我更新列表的时间。这是我的模型。py

类列表(models.Model):

list_date = models.DateTimeField('date created', blank=True, auto_now=True)
favourite = models.ManyToManyField(User, related_name="favourite", blank=True)

def __str__(self):
    return self.title

def get_absolute_url(self): 
   return reverse("user-dashboard") 


def save(self, *args, **kwargs): 
    if self.is_published and self.list_date is None:
        self.list_date = datetime.datetime.now()
    elif not self.is_published and self.list_date is not None:
        self.list_date = None
    super(Listing, self).save(*args, **kwargs)

1 个答案:

答案 0 :(得分:0)

您需要将auto_now更改为auto_now_add

list_date = models.DateTimeField('date created', blank=True, auto_now_add=True)