如何删除外键?我有两个模型: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实例的 parent 字段为None.
1 回答
手掌心
TA贡献1942条经验 获得超3个赞
尝试 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)
添加回答
举报
0/150
提交
取消