Milano
Milano

Reputation: 18745

Django - Why db doesn't raise Exception when User.email is None?

I wan't User.email field to be required. I know that blank=True will make it required inside forms but I want to force this on the database level. The weird is that Django's User model has email with null=False by default so I don't understand why I can create a user just doing:

User.objects.create(username='foo')

And the database doesn't raise any error.

Can someone explain?

Upvotes: 1

Views: 50

Answers (1)

Nico Griffioen
Nico Griffioen

Reputation: 5405

As per the Django docs, when null is False for a CharField, the default will be an empty string, User.email is an EmailField, which is a subclass of CharField, which means that creating a User without an email address will set email to ''

Upvotes: 1

Related Questions