Django 常用常见问题

本文探讨了Django模型中遇到的问题,包括在使用sqlites时,模型更新未更新`updatetime`字段的情况。指出`auto_now_add`仅在对象创建时记录时间,而`auto_now`在创建或更新时记录时间。批量更新如`QuerySet.update()`不会自动更新此字段,除非手动设置。此外,还简要提到了Django中URL参数传递的相关参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

models中,对sqlites数据更新时,updatetime不更新问题

现象:使用批量更新的.update操作,数据更新了,但是updatetime不更新。而单独更新.save()则正常更新。

说明:auto_now_add will set the field to the current time when an object is created and auto_now will set the field to the current time when an object is created or updated
auto_now_add是在创建字段的时候会自动记录创建时间。
auto_now是在更新对象和更新的时候记录更新时间。

原因:The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.
这个字段只有在调用Mode.save()时才会自动更新。当使用批量插入的QuerySet.update()不会更新该字段,你可以通过指定一个特定的值来更新该字段。

参考:https://stackoverflow.com/questions/37716134/automatically-changing-django-model-field-on-update-for-one-field-and-freezing-o
官方文档:https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.DateField

url参数传递

参考:http://blog.youkuaiyun.com/alex_chen_16/article/details/50850435

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值