我在两个模型之间使用一对一的关系,我需要能够清除这种关系.但是,我找不到清除(clear(),remove()等的方法...)删除该关系,Django管理员不会执行该操作.有没有人有这个问题的经验?我想我可能必须跳过一对一的字段并在字段上使用一对多的unique = true set.
编辑:我应该提到.我确实在该字段上设置了null = True,但它没有任何区别.
只需将其设置为无并保存:-)
my_instance.my_one_to_one_fieldname = None
my_instance.save()
Run Code Online (Sandbox Code Playgroud)
编辑:顺便说一句,这仅适用于您定义的模型OneToOneField
.我不确定你会如何向后做这件事,除了:
my_related_instance.othermodel.my_one_to_one_fieldname = None
my_related_instance.othermodel.save()
Run Code Online (Sandbox Code Playgroud)
但这与做的一样:
my_instance = my_related_instance.othermodel
Run Code Online (Sandbox Code Playgroud)
然后继续像第一个例子.