In a django project I'm trying to store a calculated decimal value, but it fails for some strange reason. My model looks something like:
class FancyModel(models.Model):
fancy_field = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True)
Sometimes, rather often actually, it crashes on save:
django.db.utils.IntegrityError: CHECK constraint failed: myapp_fancy_model
which I found out after some digging was caused by this validation:
django.core.exceptions.ValidationError: {'fancy_field': ['Ensure that there are no more than 2 decimal places.']}
I've dumped the values of the actual model, and it seems ok to me:
{'fancy_field': 3.26 }
I cannot see how this would fail. Sometimes it works fine, other times it crashes. I've tried to use the round-method, like:
model_instance.fancy_model = round(floating_value, 2)
but it still fails.
CodePudding user response:
You should consider changing the decimal_places param passed from 3 to 2
CodePudding user response:
After commenting out all the code, then adding one after one line back in, I discovered that the error wasn't caused by the decimalfield at all, but a PositiveIntegerField that got a negative value. The model.full_clean() never mentioned the PositiveIntegerField at all, so this is really weird. That's a few hours I won't get back :-/