I am using a one to one relationship between two models and I need to be able to clear that relationship. However, I cannot find a way to clear(clear(), remove(), etc...) it to remove that relationship, and the Django admin will not perform that operation. Does anyone have experience with this problem? I think I may have to skip the one to one field and use a one to many with unique=true set on the field.
Edit: I should have mentioned. I do have null=True set on the field, but it does not make a difference.
just set it to None and save :-)
my_instance.my_one_to_one_fieldname = None
my_instance.save()
edit: By the way, this only works for the model that you defined the OneToOneField
on. I'm not sure how you would go about doing this backwards, except for:
my_related_instance.othermodel.my_one_to_one_fieldname = None
my_related_instance.othermodel.save()
But that's just the same as doing:
my_instance = my_related_instance.othermodel
and then continuing like the first example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With