Django:何时使用保存后的信号自定义保存 [英] Django: When to customize save vs using post-save signal

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

问题描述

我在数据库中有一系列的测试和案例。每当测试被淘汰时,它将被终止,并且该测试的任何子案例也应该结束。我看到两种方式来实现这一点:



1)修改保存功能以结束日期子案例

2)创建一个侦听器对于测试模型被保存,然后结束日期他们的子案例。



任何理由使用另一个以外的其他?



编辑:我看到这个博客建议在您检查模型的给定值时使用保存方法。因为我正在检查end_date,也许这表明我应该使用自定义保存?



Edit2 :另外,为了记录,层次结构是协议 - >测试 - >案例 - >计划_执行,任何时候都是end_dated,每个孩子也必须是终止的。我想我最终会为每个人做基本相同的事情。



Edit3 :事实证明,为了判断当前save()是结束测试的,我需要访问旧数据和新数据,所以我使用了一个自定义保存。这是它的样子:

  def save(self):
使用自定义保存结束日期任何subCases
try:
orig = Test.objects.get(id = self.id)
enddated =(not orig.end_date)和self.end_date不是无
除了
enddated = False

super(Test,self).save()

如果结束:
for case in self.case_set .exclude(end_date__isnull = False):
case.end_date = self.end_date
case.enddater = self.enddater
case.save()

解决方案

我通常使用这个经验法则:




  • 如果您必须修改数据,以便保存不会失败,请覆盖 save()(您真的没有其他选项)。例如,在我正在开发的应用程序中,我有一个带有文本字段的模型,其中包含选项列表。这与旧代码接口,并替换具有相似文本字段但具有不同选项列表的旧型号。旧的代码有时会将我的模型从旧模型中选择出来,但是在选择之间有1:1的映射,所以在这种情况下,我可以将选择修改为新的选择。在 save()中这样做是有意义的。

  • 否则,如果保存可以无需干预就可以继续进行,保存信号。


I have a series of tests and cases in a database. Whenever a test is obsoleted, it gets end dated, and any sub-cases of that test should also be end dated. I see two ways to accomplish this:

1) Modify the save function to end date sub-cases.
2) Create a receiver which listens for Test models being saved, and then end dates their sub-cases.

Any reason to use one other than the other?

Edit: I see this blog post suggests to use the save method whenever you check given values of the model. Since I'm checking the end_date, maybe that suggests I should use a custom save?

Edit2: Also, for the record, the full hierarchy is Protocol -> Test -> Case -> Planned_Execution, and anytime one is end_dated, every child must also be endDated. I figure I'll end up doing basically the same thing for each.

Edit3: It turns out that in order to tell whether the current save() is the one that is endDating the Test, I need to have access to the old data and the new data, so I used a custom save. Here's what it looks like:

def save(self):
    """Use a custom save to end date any subCases"""
    try:
        orig = Test.objects.get(id=self.id)
        enddated = (not orig.end_date) and self.end_date is not None   
    except:
        enddated = False

    super(Test, self).save()

    if enddated:
        for case in self.case_set.exclude(end_date__isnull=False):
            case.end_date = self.end_date
            case.enddater = self.enddater
            case.save()

解决方案

I generally use this rule of thumb:

  • If you have to modify data so that the save won't fail, then override save() (you don't really have another option). For example, in an app I'm working on, I have a model with a text field that has a list of choices. This interfaces with old code, and replaces an older model that had a similar text field, but with a different list of choices. The old code sometimes passes my model a choice from the older model, but there's a 1:1 mapping between choices, so in such a case I can modify the choice to the new one. Makes sense to do this in save().
  • Otherwise, if the save can proceed without intervention, I generally use a post-save signal.

这篇关于Django:何时使用保存后的信号自定义保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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