blank is a validation-related attribute, which means it will only apply when data is entered via the django-admin site or a form derived from the model using ModelForm.
Example:
class MyForm(forms.ModelForm): class Meta: model = MyModel >>> f = MyForm({"s1":""}) >>> f.is_valid() False >>> f = MyForm({"s1":"hello world"}) >>> f.is_valid() True >>> a = f.save() >>> a <MyModel: MyModel object> >>> a.id 10
min_length
min_length does exists, but as an argument for forms.CharField not models.CharField.
class MyForm(forms.ModelForm): class Meta: model = MyModel s1 = forms.CharField(min_length=3, max_length=100) >>> f = MyForm({"s1":"12"}) >>> f.is_valid() False >>> f = MyForm({"s1":"1234"}) >>> f.is_valid() True
Raising exceptions on unspecified char fields (without forms)
By setting default=None, the charfield will default to NULL instead of "" which will raise an exception when the model is saved manually (unless null=True is specified).
class MyModel(models.Model): s1 = models.CharField(max_length=255, default=None) >>> m = MyModel() >>> m.save() Traceback (most recent call last): File "<console>", line 1, in ? <.... trimmed for brevity ...> File "/usr/lib/python2.3/site-packages/django/db/backends/sqlite3/base.py", line 193, in execute return Database.Cursor.execute(self, query, params) IntegrityError: test_mymodel.s1 may not be NULL
I've only tested this on an old-ish version of django so YMMV
Update
From 1.2 onwards, django models come with validation built-in which means you can trigger the validation functions by calling Model.full_clean(), without having to use ModelForm.
class MyModel(models.Model): s1 = models.CharField(max_length=100) def save(self, *args, **kwargs): self.full_clean() Super(MyModel, self).save(*args, **kwargs) >>> m = MyModel() >>> m.save() Traceback (most recent call last): File "<console>", line 1, in <module> File "/tmp/lsc/playground/zoo/models.py", line 42, in save self.full_clean() File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 881, in full_clean raise ValidationError(errors) ValidationError: {'s1': [u'This field cannot be blank.']}