smilebomb
smilebomb

Reputation: 5483

Django doesn't validate on create?

Is it the case that the create function doesn't validate according to the model specifications? Here's my model:

class Contact(models.Model):
    id = models.AutoField(primary_key=True)

    firstname = models.CharField(max_length=100, null=False, blank=False)
    lastname = models.CharField(max_length=100, null=False, blank=False)
    email = models.EmailField(max_length=150, blank=False, null=False)
    active = models.BooleanField(default=True)

And I use this function in my view:

new_contact = Contact.objects.create(firstname=a,
    lastname=b,
    email=c)
# where a, b, & c are empty strings in request.POST, which shouldn't validate

The call to create goes through without a problem. But, that shouldn't be the case. The model specifies those fields as required. In fact, if I go to the admin panel and view this object, I can't save it (without making any changes) because it yells at me that the fields are required.

Is it normal behavior for create to not validate?

How can I force it to validate?

Upvotes: 9

Views: 6495

Answers (4)

puroh
puroh

Reputation: 29

Django validation is mostly application level validation and not validation at DB level. Also Model validation is not run automatically on save/create of the model. If you want to validate your values at certain time in your code then you need to do it manually.

For example:

from django.core.exceptions import ValidationError
form app.models import MyModel

instance = MyModel(name="Some Name", size=15)
try:
    instance.full_clean()
except ValidationError:
    # Do something when validation is not passing
else:
    # Validation is ok we will save the instance
    instance.save()

Upvotes: 1

user566245
user566245

Reputation: 4227

  1. setting blank=False is a validation requirement and not a DB constraint.
  2. calling create, save etc. do not check validations for you.

You need to enforce the validations yourself by calling contact.full_clean()

From the documentation:

Note that full_clean() will not be called automatically when you call your model’s save() method. You’ll need to call it manually when you want to run one-step model validation for your own manually created models.

https://docs.djangoproject.com/en/3.1/ref/models/instances/#validating-objects

Upvotes: 1

ultrajohn
ultrajohn

Reputation: 2597

From Django's doc, it says that you need to explicitly call model validation related methods i.e., full_clean, clean, for model validation to take effect.

You can follow the examples available in Django documentation to implement your own validation sequence in your model.

Upvotes: 4

e4c5
e4c5

Reputation: 53734

You are mixing up forms and models.

Field.blank

If True, the field is allowed to be blank. Default is False.

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

So yes, it's normal behaviour for the model to not bother with validation of blanks.

If you want to make sure that user input of blanks doesn't end up in the database you should use a ModelForm for that. It's most un-django like to directly use request.POST

Upvotes: 1

Related Questions