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

          <bdo id='jZfg0'></bdo><ul id='jZfg0'></ul>

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

      1. 我如何创建一个带有外键的 Django 模型,它不会将删除级联到它的孩子?

        时间:2023-10-08
            • <bdo id='Us5ea'></bdo><ul id='Us5ea'></ul>
                <tbody id='Us5ea'></tbody>

            • <small id='Us5ea'></small><noframes id='Us5ea'>

                <i id='Us5ea'><tr id='Us5ea'><dt id='Us5ea'><q id='Us5ea'><span id='Us5ea'><b id='Us5ea'><form id='Us5ea'><ins id='Us5ea'></ins><ul id='Us5ea'></ul><sub id='Us5ea'></sub></form><legend id='Us5ea'></legend><bdo id='Us5ea'><pre id='Us5ea'><center id='Us5ea'></center></pre></bdo></b><th id='Us5ea'></th></span></q></dt></tr></i><div id='Us5ea'><tfoot id='Us5ea'></tfoot><dl id='Us5ea'><fieldset id='Us5ea'></fieldset></dl></div>
                  <legend id='Us5ea'><style id='Us5ea'><dir id='Us5ea'><q id='Us5ea'></q></dir></style></legend>
                  <tfoot id='Us5ea'></tfoot>
                  本文介绍了我如何创建一个带有外键的 Django 模型,它不会将删除级联到它的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的一个具有 ForeignKey 的模型实际上是其他表上的 MySQL 视图.我遇到的问题是,当我从这些表中删除数据时,Django,如 "删除对象" 文档...

                  One of my models which has ForeignKey's is actually a MySQL view on other tables. The problem I'm running into is that when I delete data from these tables, Django, as described in the "deleting objects" documentation...

                  当 Django 删除一个对象时,它模拟 SQL 的行为约束 ON DELETE CASCADE -- in换句话说,任何具有指向对象的外键被删除将被删除

                  When Django deletes an object, it emulates the behavior of the SQL constraint ON DELETE CASCADE -- in other words, any objects which had foreign keys pointing at the object to be deleted will be deleted along with it.

                  ...试图从我的视图中删除行,这当然不能,因此引发错误:

                  ...tries to remove rows from my view, which of course it can't, and so throws the error:

                  mysql_exceptions.OperationalError '>=(1395, "Can not delete from join view 'my_db.my_mysql_view'"'
                  

                  有什么方法可以在模型上指定 ForeignKey 约束,它会为我提供所有 Django 魔法,但不会级联删除到它上面?或者,有没有办法让 MySQL 忽略从我的视图中删除一行的命令而不是引发错误?

                  Is there any way to specify a ForeignKey constraint on a model which will provide me with all the Django wizardry, but will not cascade deletes onto it? Or, is there a way to ask MySQL to ignore the commands to delete a row from my view instead of raising an error?

                  推荐答案

                  Harold 的回答为我指明了正确的方向.这是我实现它的方式的草图(在法国遗留数据库上,因此命名约定有点奇怪):

                  Harold's answer pointed me in the right direction. This is a sketch on the way I implemented it (on a french legacy database, hence the slightly odd naming convention):

                  class Factures(models.Model):
                      idFacture = models.IntegerField(primary_key=True)
                      idLettrage = models.ForeignKey('Lettrage', db_column='idLettrage', null=True, blank=True)
                  
                  class Paiements(models.Model):
                      idPaiement = models.IntegerField(primary_key=True)
                      idLettrage = models.ForeignKey('Lettrage', db_column='idLettrage', null=True, blank=True)
                  
                  class Lettrage(models.Model):
                      idLettrage = models.IntegerField(primary_key=True)
                  
                      def delete(self):
                          """Dettaches factures and paiements from current lettre before deleting"""
                          self.factures_set.clear()
                          self.paiements_set.clear()
                          super(Lettrage, self).delete()
                  

                  这篇关于我如何创建一个带有外键的 Django 模型,它不会将删除级联到它的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django 模型:列的默认值 下一篇:django 字符集与 MySQL 的怪异

                  相关文章

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

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

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

                      <tfoot id='U92sX'></tfoot>
                      <legend id='U92sX'><style id='U92sX'><dir id='U92sX'><q id='U92sX'></q></dir></style></legend>