Skip to content
Advertisement

Key (id)=() is still referenced from table for onetoone field

I have 1 to 1 field in my model where i would still need to link the data with other while i delete them on the other table.In my view i am deleting model 2 instance while that is deleted i am setting the completed to Tue but it is throwing error.

models.py

JavaScript

Advertisement

Answer

There’s missing a default value or the option for the model_2_attribute to be null. If you delete Model2 then Model1 would have an invalid relation.

Option 1 would be to set the default value to null. If you delete Model2 then the model_2_attribute will be None.

JavaScript

Option 2 would be to move the completed field into Model2 and do not delete it. Instead only set the field to True.

JavaScript

You can then filter for non-completed objects like:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement