So far I haven't come across a clear explanation on blank, null, and required - in Django's models and forms.
I know the default for each is the following:
blank = False
null = False
required = True
I also know that:
blank=True
(used in models.py), means on the form level, accept empty forms - the associated field is not required in a form.
null=True
(used in models.py), means on the database level that Python None
values can be stored in the model and be saved (and then end up as SQL NULL
values in the database).
required=False
(used in forms.py), means the associated form field is not required.
Hopefully the above information will serve others well (please let me know if there are any flaws in the logic, and I will update it).
My question is the following:
When do I know when to use blank=True
vs. required=False
. If my goal is to make a form field not required, I could define this in the model using blank=True
, or I could define this in the form using required=False
. Does this mean you can define blank=True
in a model, and in the associated ModelForm
override this with required=True
?
Also related, what about when you are using a regular form (forms.Form
)? Since the form is not associated with a model (other than through view logic), what happens if again, they contradict each other?