[Django]-Saving an object with unsaved related objects

5👍

From a ticket that lead to the change

I prefer failing early and loudly, by raising an exception when an unsaved object is assigned to a related field.

I could listen to an argument for trying to re-fetch the pk from the cached related instance in save(), but that feels like action-at-a-distance: the actual problem usually happened earlier.

Automatically saving related objects is too magic; I’m sure it would be considered unexpected and undesirable in some circumstances.

My interpretation is that the developers of Django do not want people to get into the habit of things being automatically saved because that will just lead to problems at a different point in time. The explicit save of each object ensures that developers are well aware of what is being saved, and reduces accidental changes to a line of code breaking functionality.

👤Sayse

Leave a comment