django-nested-inline
django-nested-inline copied to clipboard
Boolean field preventing model to be saved
I am trying to save a model with a boolean field at level 2.
`class Level2(models.Model): id = models.AutoField(primary_key=True) level1 = models.ForeignKey(Level1, blank=True, null=True) is_correct = models.BooleanField(default=False)
class Level3(models.Model): id = models.AutoField(primary_key=True) type = models.CharField(max_length = 50, blank=True, null=True) level2 = models.ForeignKey(Level2, blank=True, null=True)`
if the default value of the boolean field (false here but same when true) is kept, this leads to the following error:
save() prohibited to prevent data loss due to unsaved related object 'level 2'
however once the model has been saved i can change the value of the field without any problem.
Is this a bug or am I missing something?
I'm not completely sure this will fix your problem, but it may.
- You don't need to explicitly declare a model's id field. Django does this automatically. It's bad practice to explicitly declare this field unless you need to modify it's behavior.
- Try adding blank=True to your boolean field.
Adding blank=True was not changing anything. But I narrowed down the problem since my post, and applied a temporary fix. The problem is that this boolean field is the only field (to be filled by the user) on level 2. So what happens is that if the user keeps the default value (for instance False), Django starts by looking at the level 2 model fields and thinks that it shouldn't save this model as there is nothing else but default values. Then it tries to save a level 3 model with a foreign key on an unsaved level 2 model. It's rather unhappy and throws the above mentioned error.
I temporarily solved the problem by changing the booleanField for a charField with choices "true" or "false" and putting null=True, blank=False. It forces the user to pick either true or false which aren't default values and therefore guarantees that the level 2 model will be saved.
But ideally there should be a mechanism in place for the level 2 model to be saved if it is referred to in a level 3 model.
Adding blank=True was not changing anything. But I narrowed down the problem since my post, and applied a temporary fix. The problem is that this boolean field is the only field (to be filled by the user) on level 2. So what happens is that if the user keeps the default value (for instance False), Django starts by looking at the level 2 model fields and thinks that it shouldn't save this model as there is nothing else but default values. Then it tries to save a level 3 model with a foreign key on an unsaved level 2 model. It's rather unhappy and throws the above mentioned error.
I temporarily solved the problem by changing the booleanField for a charField with choices "true" or "false" and putting null=True, blank=False. It forces the user to pick either true or false which aren't default values and therefore guarantees that the level 2 model will be saved.
But ideally there should be a mechanism in place for the level 2 model to be saved if it is referred to in a level 3 model.
Thank you so much for your response! Such a help, i really tried to find it out myself but your comment and solution helped me right away!
Even after 8 years 😅 GitHub is THE place
OK, how can we make a PR to fix this?