django on delete set default

Solutions on MaxInterview for django on delete set default by the best coders in the world

showing results for - "django on delete set default"
Guillaume
25 Apr 2019
1It 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:
2
3Set on_delete=CASCADE if you want the Item to be deleted when the Policy is deleted
4Set 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.
5Set 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).
6Set 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.