django - 每次我在 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)
我也成功地在管理员中注册了这个模型。但是每次我更新价格时,添加的(添加的时间)都会发生变化。请帮我解决这个问题我想存储第一次添加产品的时间,我不想改变它。
解决方案
而不是使用 auto_now=True(每当您在 admin 中调用保存时,auto_now 会将时间更改为当前时间)添加 =models.DateTimeField。使用(auto_now_add=True) 。这不会每次都更新时间
推荐阅读
- discord.js - Discord.js 事件更新存在垃圾邮件
- python - 初学者python,TypeError:+的不支持的操作数类型:'float'和'str'
- php - 为什么会出现“警告:文件(...):无法打开流”错误?
- api - UFT API 测试:如何为请求中的元素生成数字
- scala - 窗口规格/功能执行最佳方式或应首选任何替代方式
- python - Python 文件执行失败
- java - Spring-MVC / Java的汉字编码问题
- jquery - 如何在子跨度中切换课程?
- python - 这段代码片段中的“tid”究竟做了什么,它调用了线程ID吗?
- sql-server - 我需要在 Access VBA 中创建对 SQL Server 的插入传递查询