首页 > 解决方案 > 每次我在 django 项目中更改产品字段时都会更新日期

问题描述

这是我的models.py:

class Product(models.Model):

    type= models.ForeignKey(Category, on_delete = models.CASCADE)
    productid=models.CharField(max_length=30)
    name=models.CharField(max_length=30)
    fabric=models.CharField(max_length=30)
    bcode=models.CharField(max_length=4)
    Gprice=models.IntegerField(null=True,blank=True)
    added=models.DateTimeField(auto_now=True)

我也成功地在管理员中注册了这个模型。但是每次我更新价格时,添加的(添加的时间)都会发生变化。请帮我解决这个问题我想存储第一次添加产品的时间,我不想改变它。

标签: djangodjango-models

解决方案


而不是使用 auto_now=True(每当您在 admin 中调用保存时,auto_now 会将时间更改为当前时间)添加 =models.DateTimeField。使用(auto_now_add=True) 。这不会每次都更新时间


推荐阅读