Django - “last_modified”或“auto_now_add”对于一个应用程序(或多个模型?)
我知道Django有一个last_modified字段的功能(models.DateTimeField(auto_now_add=True) )..
但是假设我有一个特定的应用程序,并且我想知道其任何模型的最后一次更改是什么时候(我真的不在乎更改了哪个模型,我只想知道何时是最新的更改)这个应用程序..)
我真的必须为每个模型编写一个last_modified字段(我目前有9个模型......),然后检查每个模型哪个是最新的?
任何帮助将不胜感激:) 谢谢
I know Django has a feature of last_modified field (models.DateTimeField(auto_now_add=True)
)..
but let's say I have a certain App, and I want to know when was the last change for any of its Model (I don't really care which model was changed, I just want to know when was the latest change for this app..)
do I really have to write a last_modified field for each model (I have 9 of them for the moment...), and then check for each of them which is the latest?
any help will be appreciated :)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个定义 last_modified 字段的基类...
然后从中继承
You could create a base class that defines the last_modified field...
and then inherit from that
在《The End》中,我为我的应用程序制作了一个常量表(实际上我之前有过它用于其他用途)。
所以表看起来像这样:
并添加了一个名为“version_date”的常量。
然后,我将此代码添加到 models.py 的底部,以跟踪应用程序中所有模型的所有更改。
这样,我不需要更改我的数据库架构..只需要添加提到的代码。
谢谢大家
In The End I made a table for constants for my app (actually I had it before for use of other things).
so the Table looks like this:
and added a consant named "version_date".
Than, I added this code to the bottom of my models.py, to track all changes in all the models in the app.
This way, I don't need to change my DB Schema.. only need to add the code mentioned.
thanks all