Let's say I have this model:
class UserBook(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True)
book = models.ForeignKey(Book, on_delete=models.PROTECT)
Where the user is only allowed to borrow 1 book at a time. I want instances of this model to get deleted if the user gets deleted, but I don't want them to get deleted if a book gets deleted (by mistake, just a precaution).
What is the expected behaviour when a user gets deleted using the above constraint? I'm getting:
Cannot delete some instances of model 'UserBook' because they are referenced through a protected foreign key
Is there a way to achieve what I want? I tried to delete UserBook on pre_save
/post_save
signals with the User
as a sender but neither worked.