Django 信号与覆盖保存方法 [英] Django signals vs. overriding save method

查看:22
本文介绍了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 有几个分数",overall_score 是分数的平均值.保存评论或分数后,我需要重新计算 total_score 平均值.现在我正在使用覆盖的保存方法.使用 Django 的信号调度器有什么好处吗?

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?

推荐答案

保存/删除信号通常适用于您需要进行不完全特定于相关模型或可以应用于模型的更改的情况它们有一些共同点,或者可以配置为跨模型使用.

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.

重写的 save 方法中的一项常见任务是从模型中的某些文本字段自动生成 slug.这是一个例子,如果您需要为多个模型实现它,将受益于使用 pre_save 信号,其中信号处理程序可以采用 slug 字段的名称和生成 slug 的字段.一旦你有了类似的东西,你设置的任何增强功能也将适用于所有模型 - 例如查找您要为相关模型类型添加的 slug,以确保唯一性.

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 a pre_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.

django-mptt 为例,我使用了 pre_save 信号来管理一组字段,这些字段描述了即将创建或更新的模型的树结构,以及 pre_delete 信号来删除正在删除的对象及其树结构的详细信息它之前的对象的整个子树,它们被删除.由于使用信号,用户不必在他们的模型上添加或修改 savedelete 方法来为他们完成这个管理,他们只需要让django-mptt 知道他们希望它管理哪些模型.

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 the pre_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 modify save or delete 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 信号与覆盖保存方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆