Django 信号与重写保存方法
我很难理解这个问题。现在我有一些看起来像这样的模型:
def Review(models.Model)
...fields...
overall_score = models.FloatField(blank=True)
def Score(models.Model)
review = models.ForeignKey(Review)
question = models.TextField()
grade = models.IntegerField()
评论有几个“分数”,overall_score 是分数的平均值。保存评论或分数后,我需要重新计算总体分数平均值。现在我正在使用重写的保存方法。使用 Django 的信号调度程序有什么好处吗?
I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this:
def Review(models.Model)
...fields...
overall_score = models.FloatField(blank=True)
def Score(models.Model)
review = models.ForeignKey(Review)
question = models.TextField()
grade = models.IntegerField()
A Review is has several "scores", the overall_score is the average of the scores. When a review or a score is saved, I need to recalculate the overall_score average. Right now I'm using a overridden save method. Would there be any benefits to using Django's signal dispatcher?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您需要进行不完全特定于相关模型的更改,或者可以应用于具有共同点的模型,或者可以配置为跨模型使用时,保存/删除信号通常是有利的。
重写的 save 方法中的一项常见任务是从模型中的某些文本字段自动生成 slugs。这是一个示例,如果您需要为多个模型实现它,则可以使用
pre_save
信号,其中信号处理程序可以获取 slug 字段的名称和生成 slug 的字段。一旦您有了类似的东西,您放置的任何增强功能也将适用于所有模型 - 例如,查找您要为相关模型类型添加的段,以确保唯一性。可重用应用程序通常受益于信号的使用 - 如果它们提供的功能可以应用于任何模型,它们通常(除非不可避免)不希望用户必须直接修改其模型才能从中受益。
例如,对于 django-mptt,我使用了
pre_save
信号用于管理一组字段,这些字段描述即将创建或更新的模型的树结构,而pre_delete
信号用于删除要删除的对象及其整个子对象的树结构详细信息之前的对象树,并且它们被删除。由于使用信号,用户不必在模型上添加或修改save
或delete
方法来为他们完成此管理,他们只需要让django-mptt 知道他们希望它管理哪些模型。Save/delete signals are generally favourable in situations where you need to make changes which aren't completely specific to the model in question, or could be applied to models which have something in common, or could be configured for use across models.
One common task in overridden
save
methods is automated generation of slugs from some text field in a model. That's an example of something which, if you needed to implement it for a number of models, would benefit from using apre_save
signal, where the signal handler could take the name of the slug field and the name of the field to generate the slug from. Once you have something like that in place, any enhanced functionality you put in place will also apply to all models - e.g. looking up the slug you're about to add for the type of model in question, to ensure uniqueness.Reusable applications often benefit from the use of signals - if the functionality they provide can be applied to any model, they generally (unless it's unavoidable) won't want users to have to directly modify their models in order to benefit from it.
With django-mptt, for example, I used the
pre_save
signal to manage a set of fields which describe a tree structure for the model which is about to be created or updated and thepre_delete
signal to remove tree structure details for the object being deleted and its entire sub-tree of objects before it and they are deleted. Due to the use of signals, users don't have to add or modifysave
ordelete
methods on their models to have this management done for them, they just have to let django-mptt know which models they want it to manage.您问:
使用 Django 的信号调度程序有什么好处吗?
我在 django 文档中找到了这一点:
来自:覆盖预定义的模型方法
You asked:
Would there be any benefits to using Django's signal dispatcher?
I found this in the django docs:
From: Overriding predefined model methods
Django 文档中关于批量删除的小补充(
QuerySet
对象上的.delete()
方法):https://docs.djangoproject.com/en/1.11/ topic/db/queries/#deleting-objects
和批量更新(
QuerySet
对象上的.update()
方法):https://docs.djangoproject.com/en/2.1/ref/models/querysets/#update
Small addition from Django docs about bulk delete (
.delete()
method onQuerySet
objects):https://docs.djangoproject.com/en/1.11/topics/db/queries/#deleting-objects
And bulk update (
.update()
method onQuerySet
objects):https://docs.djangoproject.com/en/2.1/ref/models/querysets/#update
如果您使用信号,则每次保存相关评分模型时都可以更新审核评分。但如果不需要这样的功能,我认为没有任何理由将其放入信号中,这是与模型相关的东西。
If you'll use signals you'd be able to update Review score each time related score model gets saved. But if don't need such functionality i don't see any reason to put this into signal, that's pretty model-related stuff.
这是一种非规范化。看看这个漂亮的解决方案。就地组合字段定义。
It is a kind sort of denormalisation. Look at this pretty solution. In-place composition field definition.