如何使用ForeignKeys创建Django模型,它不会将删除删除到其子节点? [英] How do I create a Django model with ForeignKeys which does not cascade deletes to its children?

查看:194
本文介绍了如何使用ForeignKeys创建Django模型,它不会将删除删除到其子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


当Django删除对象时,
模拟SQL
约束ON DELETE CASCADE的行为 - 以
为单位,任何具有
外键的对象指向
的对象将被删除将与
一起删除。


...尝试从我的视图中删除行,这当然可以' t,所以抛出错误:

  mysql_exceptions.OperationalError'> =(1395,无法从连接视图中删除 my_db.my_mysql_view''

有没有办法指定一个 ForeignKey 一个模型的约束,这将为我提供所有的Django魔法,但不会级联删除o或者,有没有办法要求MySQL忽略从我的视图删除一行的命令,而不是提高错误?

解决方案
  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) :
在删除之前从当前的生产者中删除生产和零售
self.factures_set.clear()
self.paiements_set.clear()
super(Lettrage, self).delete()


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...

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'"'

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

这篇关于如何使用ForeignKeys创建Django模型,它不会将删除删除到其子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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