Hoa
Hoa

Reputation: 20438

How can I make my model fields optional in Django?

I'm trying to follow the full example at the bottom of

https://docs.djangoproject.com/en/dev/topics/auth/customizing/

In my model I have modified to the following

date_of_birth = models.DateField(null=True)

However when I try register a user I still get the following error message:

date_of_birth <ul class="errorlist"><li>This field is required.</li></ul>

In what other places do I need to make date_of_birth optional?

Upvotes: 22

Views: 24434

Answers (1)

Rohan
Rohan

Reputation: 53316

You would have to add blank=True as well in field definition.

date_of_birth = models.DateField(null=True, blank=True)

From modelform doc

If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True.

Don't forget to reset and sync DB again after changing this.

Upvotes: 42

Related Questions