Django:何时自定义保存与使用保存后信号
我的数据库中有一系列测试和案例。每当测试被废弃时,它就会被指定结束日期,并且该测试的任何子案例也应该被指定结束日期。我看到有两种方法可以实现此目的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常使用这个经验法则:
save()
(您实际上没有其他选择)。例如,在我正在开发的应用程序中,我有一个带有文本字段的模型,其中包含选项列表。它与旧代码交互,并替换具有类似文本字段但具有不同选择列表的旧模型。旧代码有时会向我的模型传递旧模型中的一个选择,但选择之间存在 1:1 映射,因此在这种情况下,我可以将选择修改为新的选择。在save()
中执行此操作是有意义的。I generally use this rule of thumb:
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 insave()
.在我的理解中,信号是模块解耦的一种手段。由于您的任务似乎只发生在一个模块中,我会自定义保存。
In my understanding, signals are a means for decoupling modules. Since your task seems to happen in only one module I'd customize save.