<tfoot id='YhtV9'></tfoot>
    • <bdo id='YhtV9'></bdo><ul id='YhtV9'></ul>
    1. <i id='YhtV9'><tr id='YhtV9'><dt id='YhtV9'><q id='YhtV9'><span id='YhtV9'><b id='YhtV9'><form id='YhtV9'><ins id='YhtV9'></ins><ul id='YhtV9'></ul><sub id='YhtV9'></sub></form><legend id='YhtV9'></legend><bdo id='YhtV9'><pre id='YhtV9'><center id='YhtV9'></center></pre></bdo></b><th id='YhtV9'></th></span></q></dt></tr></i><div id='YhtV9'><tfoot id='YhtV9'></tfoot><dl id='YhtV9'><fieldset id='YhtV9'></fieldset></dl></div>

      <small id='YhtV9'></small><noframes id='YhtV9'>

    2. <legend id='YhtV9'><style id='YhtV9'><dir id='YhtV9'><q id='YhtV9'></q></dir></style></legend>

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

        时间:2023-10-19

        <i id='k5O2O'><tr id='k5O2O'><dt id='k5O2O'><q id='k5O2O'><span id='k5O2O'><b id='k5O2O'><form id='k5O2O'><ins id='k5O2O'></ins><ul id='k5O2O'></ul><sub id='k5O2O'></sub></form><legend id='k5O2O'></legend><bdo id='k5O2O'><pre id='k5O2O'><center id='k5O2O'></center></pre></bdo></b><th id='k5O2O'></th></span></q></dt></tr></i><div id='k5O2O'><tfoot id='k5O2O'></tfoot><dl id='k5O2O'><fieldset id='k5O2O'></fieldset></dl></div>
            <tbody id='k5O2O'></tbody>
          <tfoot id='k5O2O'></tfoot>

            <small id='k5O2O'></small><noframes id='k5O2O'>

            • <bdo id='k5O2O'></bdo><ul id='k5O2O'></ul>

              1. <legend id='k5O2O'><style id='k5O2O'><dir id='k5O2O'><q id='k5O2O'></q></dir></style></legend>
                • 本文介绍了Django:何时自定义保存与使用保存后信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在数据库中有一系列测试和案例.每当一个测试被废弃时,它就会被标记为结束日期,并且该测试的任何子案例也应该被标记为结束日期.我看到了两种方法来实现这一点:

                  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) 修改保存功能以结束日期子案例.
                  2)创建一个接收器来监听正在保存的测试模型,然后结束它们的子案例的日期.

                  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?

                  我看到了这个 博客文章建议在检查模型的给定值时使用 save 方法.由于我正在检查 end_date,也许这表明我应该使用自定义保存?

                  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:另外,为了记录,完整的层次结构是 Protocol -> Test -> Case -> Planned_Execution,并且任何时候 end_dated,每个孩子也必须 endDated.我想我最终会为每个人做基本相同的事情.

                  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:原来为了判断当前的save()是否是endDating Test的那个,我需要访问旧数据和新数据,所以我使用了自定义保存.这是它的样子:

                  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:

                  • 如果您必须修改数据以使保存不会失败,请覆盖 save()(您实际上没有其他选择).例如,在我正在开发的应用程序中,我有一个模型,其中包含一个包含选项列表的文本字段.这与旧代码交互,并替换了具有相似文本字段但具有不同选项列表的旧模型.旧代码有时会通过我的模型从旧模型中选择一个选项,但选项之间存在 1:1 映射,因此在这种情况下,我可以将选项修改为新模型.在 save() 中这样做是有意义的.
                  • 否则,如果无需干预即可进行保存,我一般会使用保存后信号.
                  • 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:何时自定义保存与使用保存后信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:以非常高的质量在 Python 中保存图像 下一篇:从 0-d numpy 数组中恢复字典

                  相关文章

                    <bdo id='lNYJS'></bdo><ul id='lNYJS'></ul>
                  <tfoot id='lNYJS'></tfoot>
                    1. <small id='lNYJS'></small><noframes id='lNYJS'>

                      <i id='lNYJS'><tr id='lNYJS'><dt id='lNYJS'><q id='lNYJS'><span id='lNYJS'><b id='lNYJS'><form id='lNYJS'><ins id='lNYJS'></ins><ul id='lNYJS'></ul><sub id='lNYJS'></sub></form><legend id='lNYJS'></legend><bdo id='lNYJS'><pre id='lNYJS'><center id='lNYJS'></center></pre></bdo></b><th id='lNYJS'></th></span></q></dt></tr></i><div id='lNYJS'><tfoot id='lNYJS'></tfoot><dl id='lNYJS'><fieldset id='lNYJS'></fieldset></dl></div>
                      <legend id='lNYJS'><style id='lNYJS'><dir id='lNYJS'><q id='lNYJS'></q></dir></style></legend>