This is my simple Django class:
class MyClass(models.Model):
my_field1 = models.IntegerField(null=False,blank=False,)
# I want to insert validation rule such that my_field2 = True iff my_field1 > 10
my_field2 = models.BooleanField(null=False, blank=False,)
I would like to insert validation rule such that my_field2
= True iff my_field1
> 10. If the user enters data that violates that constraint, I would like the constructor to raise an exception.
#MyClass.object.create(9, False) # Should Succeed
#MyClass.object.create(11, True) # Should Succeed
#MyClass.object.create(31, True) # Should Succeed
#MyClass.object.create(8, True) # Should Throw exception
#MyClass.object.create(21, False) # Should Throw exception
How can I do it? Everything I have read about django validation occurs in the model form. But my application has no forms. I need the validation in the model itself. How can I do that?