blue_zinc
blue_zinc

Reputation: 2500

Django Charfield null=False Integrity 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()
> e.save()

Upvotes: 9

Views: 3397

Answers (3)

TesterToDev
TesterToDev

Reputation: 1

In the views.py you can check if the string is '' , in that case you can send NONE. That will raise the integrity error.

Thanks

Upvotes: 0

souldeux
souldeux

Reputation: 3755

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.

Upvotes: 19

Pierre.Sassoulas
Pierre.Sassoulas

Reputation: 4282

In order for your model to raise an error if a CharField is empty, you just need to override the clean function, like this :

from django.core.exceptions import ValidationError

class Discount(models.Model):
    code = models.CharField(max_length=14, unique=True)

    def clean(self):
         if self.code == '':
              raise ValidationError('Empty code not allowed')

See : How can you create a non-empty CharField in Django?

Upvotes: 0

Related Questions