blue_zinc blue_zinc -4 years ago 193
Python Question

Django Charfield null=False Inegrity Error not raised

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()

Answer Source

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 because empty string values will always be stored as empty strings, not as NULL. 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.

And on validators.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download