Django - “last_modified”或“auto_now_add”对于一个应用程序(或多个模型?)

发布于 2024-11-19 13:37:57 字数 260 浏览 5 评论 0原文

我知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-11-26 13:37:57

您可以创建一个定义 last_modified 字段的基类...

class YourBaseClassName(models.Model):
    last_modified = models.DateTimeField(auto_now=True)

然后从中继承

class AnotherClass(YourBaseClassName):
    another_field = models.CharField(max_length=50)

You could create a base class that defines the last_modified field...

class YourBaseClassName(models.Model):
    last_modified = models.DateTimeField(auto_now=True)

and then inherit from that

class AnotherClass(YourBaseClassName):
    another_field = models.CharField(max_length=50)
樱&纷飞 2024-11-26 13:37:57

在《The End》中,我为我的应用程序制作了一个常量表(实际上我之前有过它用于其他用途)。
所以表看起来像这样:

from django.db import models
from django.db.models.signals import post_save

class Constant(models.Model):
    name = models.CharField(max_length=50)
    value = models.CharField(max_length=50)

并添加了一个名为“version_date”的常量。

然后,我将此代码添加到 models.py 的底部,以跟踪应用程序中所有模型的所有更改。

myapp = models.get_app('myapp')
models2track = models.get_models(myapp)
def update_version(sender, **kwargs):
    for model in models2track:
        post_save.disconnect(update_version, sender=model, dispatch_uid="some_uid"+model._meta.db_table)

    version_date = Constant.objects.get_or_create(id=1,name="version date")[0]
    version_date.value = str(int(time.time()))
    version_date.save()

    for model in models2track:
        post_save.connect(update_version, sender=model, dispatch_uid="some_uid"+model._meta.db_table)

for model in models2track:
    post_save.connect(update_version, sender=model, dispatch_uid="some_uid"+model._meta.db_table)

这样,我不需要更改我的数据库架构..只需要添加提到的代码。
谢谢大家

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:

from django.db import models
from django.db.models.signals import post_save

class Constant(models.Model):
    name = models.CharField(max_length=50)
    value = models.CharField(max_length=50)

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.

myapp = models.get_app('myapp')
models2track = models.get_models(myapp)
def update_version(sender, **kwargs):
    for model in models2track:
        post_save.disconnect(update_version, sender=model, dispatch_uid="some_uid"+model._meta.db_table)

    version_date = Constant.objects.get_or_create(id=1,name="version date")[0]
    version_date.value = str(int(time.time()))
    version_date.save()

    for model in models2track:
        post_save.connect(update_version, sender=model, dispatch_uid="some_uid"+model._meta.db_table)

for model in models2track:
    post_save.connect(update_version, sender=model, dispatch_uid="some_uid"+model._meta.db_table)

This way, I don't need to change my DB Schema.. only need to add the code mentioned.
thanks all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文