在 Django 中向多个模型添加常见 date_added、date_modified 的最佳方法

发布于 2024-08-15 13:04:36 字数 1173 浏览 6 评论 0原文

我将 date_added 和 date_modified 字段添加到当前项目中的一堆常见模型中。我正在子类化 models.Model 并添加适当的字段,但我想添加自动保存行为(即:每次有人调用 MyModel.save() 时,date_modified 字段都会更新。我看到两种方法:覆盖 save() 方法或者在抽象基类中添加 pre_save 信号处理程序,

class CommonData(models.Model):
    date_added = models.DateTimeField(default=datetime.datetime.today,null=False,blank=False)
    date_modified = models.DateTimeField(default=datetime.datetime.today,null=True,blank=True)

    # register a handler for the pre_save to update date_modified
    def pre_save_handler(sender, **kwargs):
        date_modified = datetime.datetime.today

    def __init__():
        pre_save.connect(pre_save_handler, sender=self)

或者

class CommonData(models.Model):
    date_added = models.DateTimeField(default=datetime.datetime.today,null=False,blank=False)
    date_modified = models.DateTimeField(default=datetime.datetime.today,null=True,blank=True)

    # overriding save 
    def save(force_insert=False,force_update=False):
        date_modified = datetime.datetime.now
        return models.Model.save(force_insert, force_update)

我是 Django 和 Python 的新手,想知道哪种方法更“django”?哪种方法更有效?

I am adding date_added and date_modified fields to a bunch of common models in my current project. I am subclassing models.Model and adding the appropriate fields, but I want to add automated save behavior (i.e: evey time anyone calls MyModel.save(), the date_modified field gets updated. I see two approaches: overriding the save() method or adding a pre_save signal handler in the abstract base class.

class CommonData(models.Model):
    date_added = models.DateTimeField(default=datetime.datetime.today,null=False,blank=False)
    date_modified = models.DateTimeField(default=datetime.datetime.today,null=True,blank=True)

    # register a handler for the pre_save to update date_modified
    def pre_save_handler(sender, **kwargs):
        date_modified = datetime.datetime.today

    def __init__():
        pre_save.connect(pre_save_handler, sender=self)

or

class CommonData(models.Model):
    date_added = models.DateTimeField(default=datetime.datetime.today,null=False,blank=False)
    date_modified = models.DateTimeField(default=datetime.datetime.today,null=True,blank=True)

    # overriding save 
    def save(force_insert=False,force_update=False):
        date_modified = datetime.datetime.now
        return models.Model.save(force_insert, force_update)

I'm new to Django and Python and wondered which approach was more "django"? Which is more efficient? which is the "right" way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

掀纱窥君容 2024-08-22 13:04:36

由于您是 Django 新手,您可能会发现 Django 命令扩展很有用:

http: //code.google.com/p/django-command-extensions/

...其中方便地包含一个 TimeStampedModel,您可以从中派生模型:

http://code.google.com/p/django-command-extensions/wiki/ModelExtensions

提供自我管理的“创建”和“修改”字段的抽象基类模型。

Since you're new to Django, you might find the Django Command Extensions useful:

http://code.google.com/p/django-command-extensions/

... which conveniently includes a TimeStampedModel you can derive your models from:

http://code.google.com/p/django-command-extensions/wiki/ModelExtensions

An abstract base class model that provides self-managed "created" and "modified" fields.

清音悠歌 2024-08-22 13:04:36

您是否尝试查看 DateTimeFieldauto_now=Trueauto_now_add=True ?它们会自动执行您需要的操作。否则,保存覆盖和信号处理之间没有真正的区别 - 事实上,pre_save 信号是从 django 模型的 save 方法调用的。

文档: http://docs.djangoproject.com/en/dev /ref/models/fields/#datefield

Did you try looking at DateTimeField's auto_now=True and auto_now_add=True? They do just what you need automatically. Otherwise, there is no real difference between doing save override and signal handling - in fact, pre_save signal is being called from django model's save method.

Docs: http://docs.djangoproject.com/en/dev/ref/models/fields/#datefield

屌丝范 2024-08-22 13:04:36

您可以在 抽象基类 中定义它们,然后继承于此。这有点像有一个 MixIn 也定义了模型字段。

You can define these in an Abstract Base Class and then inherit from that. It's sort of like having a MixIn that also defines model fields.

无戏配角 2024-08-22 13:04:36

请注意,auto_now_addauto_now 使用 pre_save,在 bulk_createupdate 时不起作用。因此,例如,在 MySQL 中,date_added 字段将为“0000-00-00 00:00:00”,并且可能会收到警告:“警告:列“date_added”不能为空”。所以你可以使用 auto_now* 但你应该小心。

Note that auto_now_add and auto_now uses pre_save, which not working when bulk_create or update. Thus in your MySQL, for example, date_added field will be '0000-00-00 00:00:00' and it is possible to get warning: 'Warning: Column 'date_added' cannot be null'. So you can use auto_now* but you should be careful.

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