I have a model:
class Discount(models.Model):
code = models.CharField(max_length=14, unique=True, null=False, blank=False)
email = models.EmailField(unique=True)
discount = models.IntegerField(default=10)
In my shell when I try and save a Discount object with no input, it doesn't raise an error. What am I doing wrong?
> e = Discount()
> e.save()
No default Django behavior will save CHAR
or TEXT
types as Null
- it will always use an empty string (''
). null=False
has no effect on these types of fields.
blank=False
means that the field will be required by default when the model is used to render a ModelForm. It does not prevent you from manually saving a model instance without that value.
What you want here is a custom model validator:
from django.core.exceptions import ValidationError
def validate_not_empty(value):
if value == '':
raise ValidationError('%(value)s is empty!'), params={'value':value})
Then add the validator to your model:
code = models.CharField(max_length=14, unique=True, validators=[validate_not_empty])
This will take care of the form validation you want, but validators don't automatically run when a model instance is saved. Further reading here. If you want to validate this every time an instance is saved, I suggest overriding the default save
behavior, checking the value of your string there, and interrupting the save by raising an error if necessary. Good post on overriding save
here.
Further reading on null
:
Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.
And on validators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With