I have a model:
class Discount(models.Model):
code = models.CharField(max_length=14, unique=True, null=False, blank=False)
discount = models.IntegerField(default=10)
> 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..