Django:何时自定义保存与使用保存后信号

发布于 2024-10-31 05:43:59 字数 1282 浏览 1 评论 0原文

我的数据库中有一系列测试和案例。每当测试被废弃时,它就会被指定结束日期,并且该测试的任何子案例也应该被指定结束日期。我看到有两种方法可以实现此目的:

1)修改保存函数以结束日期子情况。
2)创建一个接收器,监听正在保存的测试模型,然后确定其子案例的结束日期。

有什么理由使用其中之一而不是另一个?

编辑:我看到这个博客文章建议每当您检查模型的给定值时都使用 save 方法。由于我正在检查结束日期,也许这表明我应该使用自定义保存?

Edit2:此外,根据记录,完整的层次结构是 Protocol ->测试->案例-> Planned_Execution,只要有一个是 end_dated,每个子项也必须是 endDated。我想我最终会为每个人做基本上相同的事情。

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

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 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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2024-11-07 05:43:59

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

  • 如果您必须修改数据以使保存不会失败,则重写 save() (您实际上没有其他选择)。例如,在我正在开发的应用程序中,我有一个带有文本字段的模型,其中包含选项列表。它与旧代码交互,并替换具有类似文本字段但具有不同选择列表的旧模型。旧代码有时会向我的模型传递旧模型中的一个选择,但选择之间存在 1:1 映射,因此在这种情况下,我可以将选择修改为新的选择。在 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.
戏蝶舞 2024-11-07 05:43:59

在我的理解中,信号是模块解耦的一种手段。由于您的任务似乎只发生在一个模块中,我会自定义保存。

In my understanding, signals are a means for decoupling modules. Since your task seems to happen in only one module I'd customize save.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文