简体   繁体   English

有没有办法让updateview中的时间与django中createview中的时间保持一致

[英]is there a way to keep the time in updateview same as the time in createview in django

i have realised that when i run the updateview the listdate changes to the time I updated the listing.Here is my models.py我意识到当我运行 updateview 时,listdate 更改为我更新列表的时间。这是我的 models.py

class Listing(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)

您需要将auto_now更改为auto_now_add

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM