Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean field preventing model to be saved #56

Open
clemyvi opened this issue Feb 5, 2016 · 3 comments
Open

Boolean field preventing model to be saved #56

clemyvi opened this issue Feb 5, 2016 · 3 comments

Comments

@clemyvi
Copy link

clemyvi commented Feb 5, 2016

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?

@FFX01
Copy link

FFX01 commented Feb 11, 2016

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.

@clemyvi
Copy link
Author

clemyvi commented Feb 11, 2016

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.

@GergelyKovach
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants