I’d like to make sure that an integer field in my model never gets below a certain amount, is there a way to add some sort of constraint in Django? Or do I need to add a max() around all places I check for the value?
I’d like to make sure that an integer field in my model never gets
Share
There are two ways:
First one in form validation.
The second one in the model validation (for the model forms).
updated
If you don’t use forms, then you can overwrite save() method or write code in pre-save signal:
don’t forget to enclose save() method into a
tryclause:If you don’t like signals, then overwrite save method: