1👍
Django tracks the object you’re trying to edit or add only by a primary_key
. If the newly set primary key matches some object in the database, this object will be replaced when saving the newly created object. Similarly, if you change your primary key and save the object, Django will not update the old one, but create the new one (or replace newly matching one, if applicable) instead.
As you are using your OneToOneField
as a primary key for your object, what you want to achieve is not possible without a complex, additional logic when saving your object.
Instead, you can just create additional field as your primary key (or let Django do it for you by not using primary_key=True
in any of the fields in your model), so Django can properly track by itself which object are you editing.