I have 3 models: User
, UserItem
(the m2m thourgh), and Item
.
A User
can create an Item
. This automatically creates a UserItem
.
A different User
can see that Item
, and add it to their own list of items, creating another UserItem
.
If that first User
wants to delete the Item
, the other User
won't be happy - it needs to stay, but appear gone for the initial User
. However, if there's only one User
still related to it, then the Item
is safe to delete, and should be deleted to avoid filling the database with dead records.
This is how I think it should be handled:
Item delete
call made to API fromUser
Item
pre_delete
checks ifitem.user_set > 1
- If
True
, manually delete theUserItem
, leaveItem
where it is. IfFalse
, delete theItem
This way UserItem
isn't exposed via the API, and management for a client is simplified.
Is this the right/common way to go? How can it be done with Django? I'm unsure how to prevent Item.delete()
from happening within pre_delete
without raising an exception, but as this is expected behaviour raising an exception doesn't seem like the right way to do this.