[Answered ]-How can I delete a django reverse OneToOne-Relation and then reuse the model instance?

2👍

✅

Calling refresh_from_db of the related instance should do. You can make it transparent by overriding delete of the dependent model.

class OtherModel(models.Model):
    onetoone = OneToOneField(MyModel)

    def delete(self, using=None, keep_parents=False):
        result = self.delete(using, keep_parents)
        self.onetoone.refresh_from_db()
        return result

0👍

The hasattr will always return True, even if you’ve never created the relationship in the first place. It is not the right thing to be using here.

Instead, you need to check whether there is a related object.

try:
    obj.onetoone
except OtherModel.DoesNotExist:
    print("does not exist")

Leave a comment