Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank, Null, and Required in Django Models and ModelForms

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?

like image 981
Joker Avatar asked Jun 19 '13 18:06

Joker


People also ask

What is NULL and blank in Django models?

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.

What is NULL true and blank true in Django?

null=True will set the field's value to NULL i.e., no data. It is basically for the databases column value. blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms.

How do I allow NULL values in Django model?

The Django convention is to use the empty string, not NULL. The default values of null and blank are False. Also there is a special case, when you need to accept NULL values for a BooleanField , use NullBooleanField instead.

What is required field in Django model?

How to use required in Django Form field? required is often used to make the field optional that is the user would no longer be required to enter the data into that field and it will still be accepted.


1 Answers

The contradictions don't matter. I think this brings flexibility to the development of a Django application. For example, if you're using a third party library that defines some models, and they set blank=True, but for other purposes you need that field is mandatory, then in your forms you can fix it without touching the third party library code.

This just add some flexibility to the framework IMHO, that brings no harm. You can use it for your own purposes. The contradictions are not significant.

Hope this helps!

like image 100
Paulo Bu Avatar answered Sep 23 '22 05:09

Paulo Bu