django on delete set default code example
Example: django on delete set default
It depends on your business requirements. Should every Item always point to a valid Policy? And what's the requirement from business point-of-view when a Policy gets deleted? Should the Items pointing to it also be deleted? We don't know your requirements, so it's difficult to answer your question. These are your options from a technical perspective:
Set on_delete=CASCADE if you want the Item to be deleted when the Policy is deleted
Set on_delete=PROTECT if you don't want to allow any Policy to be deleted if there's still any Item pointing to it. In that case you'll have to try: policy.delete(); except ProtectedError: ... in your code to handle that situation.
Set on_delete=SET_DEFAULT if you know your default policy will not be deleted (you could override the delete method on Policy to avoid deleting the default policy).
Set on_delete=SET_NULL if an Item can have no Policy. That may be valid in certain business scenarios. But in this case you must also have null=True.