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

查看:95
本文介绍了Django信号与覆盖保存方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

围绕这个问题我遇到麻烦。现在我有一些看起来像这样的模型:

  def Review(models.Model)
.. .fields ...
overall_score = models.FloatField(blank = True)

def Score(models.Model)
review = models.ForeignKey(Review)
问题= models.TextField()
grade = models.IntegerField()

有几个分数,总评分是得分的平均值。当保存评论或得分时,我需要重新计算总体评分平均值。现在我使用一个重写的保存方法。使用Django的信号调度程序会有什么好处吗?

解决方案

保存/删除信号在需要做的情况下通常是有利的或者可以将其应用于具有共同点的模型,或者可以配置为在模型之间使用。



一个覆盖 save 方法中的常见任务是从模型中的某些文本字段自动生成s。。这是一个例子,如果您需要为多个模型实现,可以使用 pre_save 信号,其中信号处理程序可以使用slug字段和字段的名称来生成slug。一旦你有这样的东西,你所建立的任何增强的功能也将适用于所有的模型 - 例如查看您要添加的有关模型类型的s,,以确保唯一性。



可重复使用的应用程序通常会受益于使用信号 - 如果功能他们提供可以应用于任何模型,他们一般(除非是不可避免的)不希望用户必须直接修改他们的模型才能从中受益。



例如, django-mptt ,我使用了 pre_save 信号来管理描述将要创建或更新的模型的树结构的一组字段,并且 pre_delete 信号以删除树要删除的对象的结构细节及其整个对象的子树,并将其删除。由于使用信号,用户不必在其模型上添加或修改 save delete 方法为他们做这个管理,他们只需要让django-mptt知道他们想要管理哪些模型。


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?

解决方案

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

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天全站免登陆