Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my model fields optional in Django?

Tags:

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?

like image 502
Hoa Avatar asked May 30 '13 05:05

Hoa


People also ask

How do I make integer field optional in Django?

Use null=True and blank=True in your model.

How do you make a field optional in Python?

A Python optional argument is a type of argument with a default value. You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional.


1 Answers

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.

like image 59
Rohan Avatar answered Oct 13 '22 03:10

Rohan