在 Django 中向多个模型添加常见 date_added、date_modified 的最佳方法
我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您是 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
您是否尝试查看
DateTimeField
的auto_now=True
和auto_now_add=True
?它们会自动执行您需要的操作。否则,保存覆盖和信号处理之间没有真正的区别 - 事实上,pre_save 信号是从 django 模型的 save 方法调用的。文档: http://docs.djangoproject.com/en/dev /ref/models/fields/#datefield
Did you try looking at
DateTimeField
'sauto_now=True
andauto_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
您可以在 抽象基类 中定义它们,然后继承于此。这有点像有一个 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.
请注意,
auto_now_add
和auto_now
使用pre_save
,在bulk_create
或update
时不起作用。因此,例如,在 MySQL 中,date_added 字段将为“0000-00-00 00:00:00”,并且可能会收到警告:“警告:列“date_added”不能为空”。所以你可以使用 auto_now* 但你应该小心。Note that
auto_now_add
andauto_now
usespre_save
, which not working whenbulk_create
orupdate
. 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.