[英]How to unbind the foreign key related relationship?
如何刪除外鍵?
我有兩個模型:
class Child(models.Model):
name = models.CharField(max_length=256, null=True, blank=True)
parent = models.ForeignKey(to=Parent, null=True, related_name="children", on_delete=models.DO_NOTHING)
class Parent(models.Model):
name = models.CharField(max_length=256, null=True, blank=True)
def unbind_children(self): # there I want to unbind all children
# how to realize this?
我想取消綁定該子項,這意味着我要將特殊的Parent關聯Child
實例的父字段設為None
。
嘗試self.children.update(parent=None)
class Parent(models.Model):
name = models.CharField(max_length=256, null=True, blank=True)
def unbind_child(self): self.children.update(parent=None)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.